Qt词典搜索
2024-10-15 08:46:43
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 character
if (strDictLookupKey.contains(QRegExp("[A-Za-z0-9]")))
{
emit DictLookUpKeyFinished(false, "---NO_SUPPORT---");
return;
} if (strDictLookupKey.size() >= )
{
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 key
strLookUpUrl = 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 result
if (!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 == )
{
QJsonObject resultJsonObj = jsonObj.value("result").toObject();
if (!resultJsonObj.value("words").toString().isEmpty())
{
//CiHai search result
strTextResult = 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 result
QJsonArray resultArray = jsonObj.value("result").toArray();
if (resultArray.size() > )
{
QJsonObject resultArrayJsonObj = resultArray.at().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);
}
最新文章
- hdu 1002.A + B Problem II 解题报告
- NIS 报错No such map passwd.byname. Reason: Can&#39;t bind to server which serves this domain
- Java实现堆排序(大根堆)
- jquery时间倒计时
- 制作3D图片立方体旋转特效
- ASP.NET MVC 开源项目学习之ProDinner (一)
- 关于IE下AJAX的问题探讨
- 编译安装HTTPD 2.4.9版本
- 用adb pull命令从android系统中读取文件失败的原因及解决办法
- WCF服务实现客户端Cookie共享,表单验证的解决方案
- Java中不定参的使用规则
- 2016年团体程序设计天梯赛-决赛 L1-7. 到底是不是太胖了(10)
- path和classpath的区别
- JS奇淫巧技:防抖函数与节流函数
- C++: 可变参数;
- Eclipse 插件Maven在使用 add dependency,找不到包,解决办法
- <;hr>; 水平样式分隔线
- testNG 学习笔记 Day 1 使用功能详解
- RBAC相关的配置
- Docker实战(四)之Docker数据管理