日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt词典搜索

發布時間:2023/12/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的Qt词典搜索的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。