Qt词典搜索
Qt詞典搜索
采用阿凡達數據-API數據接口及愛詞霸API數據接口實現詞典搜索功能,實例字符串搜索接口分別為:中文詞組采用“詞典”,中文單個字采用“中華字典”,英文或其他字符采用“愛詞霸”;
對應的API接口:
http://api.avatardata.cn/CiHai/LookUp?key=[申請的APPKEY]&keyword=[搜索內容]
http://api.avatardata.cn/XinHuaZiDian/LookUp?key=[申請的APPKEY]&content=[搜索內容]
http://dict-co.iciba.com/api/dictionary.php?key=[申請的APPKEY]&w=[搜索內容]
1.字符串搜索分類
void DictLookUpKeyRequest(const QString &strSearchKey) {QString strDictLookupKey = strSearchKey.trimmed();if (strDictLookupKey.isEmpty()){emit DictLookUpKeyFinished(false, "---INVALID_URL---");return;}QString strLookUpUrl = ""; if (strDictLookupKey.contains(QRegExp("[\\x4e00-\\x9fa5]"))){//search key contains Chinese characterif (strDictLookupKey.contains(QRegExp("[A-Za-z0-9]"))){emit DictLookUpKeyFinished(false, "---NO_SUPPORT---");return;}if (strDictLookupKey.size() >= 2){strLookUpUrl = QString("http://api.avatardata.cn/CiHai/LookUp?key=d19e2a742a7b467c83a63bdd263b371b&keyword=%1").arg(strSearchKey); }else{strLookUpUrl = QString("http://api.avatardata.cn/XinHuaZiDian/LookUp?key=175dcbe2c06e49db9eef7f76045ddb55&content=%1").arg(strSearchKey);}}else{//search other keystrLookUpUrl = QString("http://dict-co.iciba.com/api/dictionary.php?key=B602AC1E01426961CADF60B71CA97484&w=%1").arg(strSearchKey);}HttpDictLookUpRequest(strLookUpUrl); //QNetworkRequest }2.QNetworkRequest搜索網絡請求
void HttpDictLookUpRequest(const QUrl& url) {QNetworkReply *reply = mpManager->get(QNetworkRequest(url));connect(reply, &QNetworkReply::finished, this, [this](){QNetworkReply* reply = static_cast<QNetworkReply*>(sender());reply->deleteLater();if (reply->error()){emit HttpGetRequestFinished(false, "");}else{emit HttpGetRequestFinished(true, reply->readAll());}}); }3.搜索返回數據處理
void OnDictLookUpResult(bool bFinsihed, const QString& DictLookUpKeyReplyStr) {QString strTextResult = QString("< font color=#6e6e6e >%1</font>").arg(tr("The query word is not entered in the dictionary, please try again later"));if (bFinsihed){if (DictLookUpKeyReplyStr.contains("</dict>")){//iciba search resultif (!DictLookUpKeyReplyStr.contains("<acceptation>")){mpTextEdit->setText(strTextResult);return;}QXmlStreamReader reader(DictLookUpKeyReplyStr);while (!reader.atEnd()){reader.readNext();if (reader.isStartElement()){if (reader.name().contains("key")){strTextResult = "";strTextResult.append(QString("< font color=#4a4a4a ><b>%1</b></font>").arg(reader.readElementText()));}else if (reader.name().contains("pos") || reader.name().contains("acceptation")){strTextResult.append(QString("< font color=#6e6e6e ><br>%1</font>").arg(reader.readElementText()));} }}}else{QJsonParseError jsonParseError;QJsonDocument jsonDocument = QJsonDocument::fromJson(DictLookUpKeyReplyStr.toUtf8(), &jsonParseError);if (jsonParseError.error == QJsonParseError::NoError){QJsonObject jsonObj = jsonDocument.object();int returnValue = jsonObj.value("return_code").toInt();if (returnValue == 0){QJsonObject resultJsonObj = jsonObj.value("result").toObject();if (!resultJsonObj.value("words").toString().isEmpty()){//CiHai search resultstrTextResult = QString("< font color=#4a4a4a ><b>%1</b></font>").arg(resultJsonObj.value("words").toString());strTextResult.append(QString("< font color=#6e6e6e ><br>%1</font>").arg(resultJsonObj.value("content").toString()));}else{//XinHuaZiDian search resultQJsonArray resultArray = jsonObj.value("result").toArray(); if (resultArray.size() > 0){QJsonObject resultArrayJsonObj = resultArray.at(0).toObject();strTextResult = QString("< font color=#4a4a4a ><b>%1</b></font>").arg(resultArrayJsonObj.value("hanzi").toString());if (!resultArrayJsonObj.value("jianjie").toString().isEmpty()){strTextResult.append(QString("< font color=#6e6e6e ><br>%1</font>").arg(resultArrayJsonObj.value("jianjie").toString()));}if (!resultArrayJsonObj.value("xiangjie").toString().isEmpty()){strTextResult.append(QString("< font color=#6e6e6e ><br>%1</font>").arg(resultArrayJsonObj.value("xiangjie").toString()));} }}}}} }mpTextEdit->setText(strTextResult); }?
轉載于:https://www.cnblogs.com/sz-leez/p/5910443.html
總結
- 上一篇: 地方时太阳时html源码,地方时、区时和
- 下一篇: Pinyin 拼音转换工具类