当前位置:   article > 正文

QT小项目-基于百度API的在线词典_qt 百度翻译api文档解读csdn

qt 百度翻译api文档解读csdn

摘要:

基于百度api的在线词典是优衣酱结合百度API开发文档和网上教程JSON花了两天完成的,项目非常简陋,功能得益于百度翻译强大的翻译能力。现在发布到博客上,供大家交流学习。

项目演示:

项目演示

说明:共包含两个界面:登录界面和翻译界面。

登录界面,需要输入appid和密匙,基于开发文档要求,要通过salt来获得签名,界面上salt一栏可以自定义输入0到2147483648任意的数值,也可以留空让程序自己生成32位二进制转十进制的随机数。

翻译界面,编辑发送区,点击翻译按钮,可以将翻译文本回显在接收区,默认中文转英文,可以自定义转换方式,但是必须遵照支持文档的要求来。

代码:

electronicdictionary.h

  1. #ifndef ELECTRONICDICTIONARY_H
  2. #define ELECTRONICDICTIONARY_H
  3. #include <QWidget>
  4. #include <QNetworkRequest> //http网址管理类
  5. #include <QNetworkReply> //网页回复数据管理类
  6. #include <QNetworkAccessManager> //http请求方式管理类
  7. namespace Ui {
  8. class electronicDictionary;
  9. }
  10. class electronicDictionary : public QWidget
  11. {
  12. Q_OBJECT
  13. public:
  14. explicit electronicDictionary(QWidget *parent = 0,QString appid = "", QString key = "" , QString salt = "");
  15. ~electronicDictionary();
  16. private slots:
  17. void on_teSend_textChanged();
  18. void on_pbTranslate_clicked();
  19. void httpReply(QNetworkReply *);
  20. void on_leFrom_editingFinished();
  21. void on_leTo_editingFinished();
  22. void on_pbSwitch_clicked();
  23. private:
  24. Ui::electronicDictionary *ui;
  25. QString url;
  26. QString q;
  27. QString from;
  28. QString to;
  29. QString salt;
  30. QString sign;
  31. QString signMd5;
  32. QString appid;
  33. QString key;
  34. QNetworkRequest *request;
  35. QNetworkAccessManager *manager;
  36. QString networkReply;
  37. QJsonObject stringToJson(const QString jsonString);
  38. void md5Encryption();
  39. void urlEncode();
  40. };
  41. #endif // ELECTRONICDICTIONARY_H

widget.h

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include "electronicdictionary.h"
  5. namespace Ui {
  6. class Widget;
  7. }
  8. class Widget : public QWidget
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit Widget(QWidget *parent = 0);
  13. ~Widget();
  14. private slots:
  15. void on_leAppId_editingFinished();
  16. void on_leKey_editingFinished();
  17. void on_pbEncryption_clicked();
  18. void on_leSalt_editingFinished();
  19. private:
  20. Ui::Widget *ui;
  21. QString salt;
  22. QString appid;
  23. QString key;
  24. electronicDictionary * dictionary;
  25. };
  26. #endif // WIDGET_H

electronicdictionary.cpp

  1. #include "electronicdictionary.h"
  2. #include "ui_electronicdictionary.h"
  3. #include <QDebug>
  4. #include <QJsonDocument>
  5. #include <QJsonObject>
  6. #include <QJsonParseError>
  7. #include <QJsonArray>
  8. #include <QVariant>
  9. #include <QTextCodec>
  10. electronicDictionary::electronicDictionary(QWidget *parent,QString appid, QString key, QString salt):
  11. QWidget(parent),
  12. ui(new Ui::electronicDictionary)
  13. {
  14. ui->setupUi(this);
  15. //默认初始化
  16. from = "en";
  17. to = "zh";
  18. this->appid = appid;
  19. this->key = key;
  20. this->salt = salt;
  21. }
  22. electronicDictionary::~electronicDictionary()
  23. {
  24. delete ui;
  25. }
  26. void electronicDictionary::on_teSend_textChanged()
  27. {
  28. q = ui->teSend->toPlainText();
  29. }
  30. void electronicDictionary::on_pbTranslate_clicked()
  31. {
  32. md5Encryption();
  33. // urlEncode();
  34. //设置访问地址
  35. url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
  36. url += "q=" + q +
  37. "&from=" + from +
  38. "&to=" + to +
  39. "&appid=" + appid +
  40. "&salt=" + salt +
  41. "&sign=" + signMd5;
  42. qDebug() << "url=" << url;
  43. //实例化网址管理
  44. request = new QNetworkRequest;
  45. //1、设置具体的网址
  46. request->setUrl(url);
  47. //2、实例化http请求方式
  48. manager = new QNetworkAccessManager;
  49. //关联信号 finished, 当网页回复数据时触发,此时才能在槽函数中,读取请求到的数据
  50. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(httpReply(QNetworkReply *)));
  51. //3、get方法请求(get方法上传)
  52. manager->get(*request);
  53. }
  54. void electronicDictionary::httpReply(QNetworkReply * reply)
  55. {
  56. networkReply = "";
  57. networkReply.append(reply->readAll());
  58. qDebug() << "networkReply" << networkReply;
  59. QJsonObject obj = stringToJson(networkReply);
  60. if(obj.contains("trans_result"))
  61. {
  62. QJsonValue val = obj.value("trans_result");
  63. if(val.isArray())
  64. {
  65. QJsonArray array = val.toArray();
  66. // qDebug() << array; //将该对象一次性打印出来
  67. qDebug() << array.at(0);
  68. if(array.at(0).isObject())
  69. {
  70. QJsonObject obj = array.at(0).toObject();
  71. if(obj.contains("dst"))
  72. {
  73. QString utf8Reply = obj.value("dst").toString();
  74. qDebug() << "utf8Reply" << utf8Reply;
  75. ui->teRecv->clear();
  76. ui->teRecv->insertPlainText(utf8Reply);
  77. }
  78. }
  79. }else{
  80. qDebug() << "错误:"<<val;
  81. }
  82. }
  83. }
  84. QJsonObject electronicDictionary::stringToJson(const QString jsonString)
  85. {
  86. //QTextCodec类提供文本编码之间的转换
  87. //codecForName():搜索所有已安装的qtextcodec对象,并返回与名称最匹配的对象;
  88. //匹配是不区分大小写的。如果找不到与名称匹配的编解码器,则返回0。
  89. QTextCodec *tc = QTextCodec::codecForName("UTF-8");//防止中文乱码
  90. //指示编译器指定名称的参数不在函数正文中使用。这可用于抑制编译器警告,同时允许在其签名中使用有意义的参数名称定义函数。
  91. Q_UNUSED(tc);
  92. QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonString.toUtf8());
  93. if (jsonDocument.isNull()){
  94. qDebug() << "数据解析有误"; //判断是否解析出问题
  95. }
  96. QJsonObject jsonObject = jsonDocument.object();
  97. // qDebug() << "jsonObject" << jsonObject;
  98. return jsonObject;
  99. }
  100. void electronicDictionary::on_leFrom_editingFinished()
  101. {
  102. from = ui->leFrom->text();
  103. qDebug() << "from=" << from;
  104. }
  105. void electronicDictionary::on_leTo_editingFinished()
  106. {
  107. to = ui->leTo->text();
  108. qDebug() << "to=" << to;
  109. }
  110. void electronicDictionary::on_pbSwitch_clicked()
  111. {
  112. QString tmp = from;
  113. from = to;
  114. to = tmp;
  115. ui->leFrom->setText(from);
  116. ui->leTo->setText(to);
  117. qDebug() << "from=" << from << ":to=" << to;
  118. }
  119. void electronicDictionary::md5Encryption()
  120. {
  121. sign = appid+q+salt+key;
  122. qDebug() << "sign = " << sign;
  123. //md5加密
  124. //构造一个对象,该对象可根据指定method和之后添加的data,创建出加密散列。
  125. QCryptographicHash md(QCryptographicHash::Md5);
  126. md.addData(sign.toLatin1());
  127. signMd5 = "";
  128. //返回最后的散列值。
  129. //返回字节数组的十六进制编码副本。十六进制编码使用数字0-9和字母a-f。
  130. //将字节数组附加到此字符串。
  131. signMd5.append(md.result().toHex());
  132. ui->leMd5->setText(signMd5);
  133. qDebug() << "signMd5=" << signMd5;
  134. }
  135. void electronicDictionary::urlEncode()
  136. {
  137. QTextCodec *tc = QTextCodec::codecForName("UTF-8");
  138. Q_UNUSED(tc);
  139. QByteArray byteArrayUTF8 = tc->fromUnicode(q);
  140. q.clear();
  141. q = QString(byteArrayUTF8.toPercentEncoding());
  142. qDebug() << "URL encode q=" << q;
  143. }

widget.cpp

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QDebug>
  4. #include <QRandomGenerator>
  5. #include <QCryptographicHash>
  6. Widget::Widget(QWidget *parent) :
  7. QWidget(parent),
  8. ui(new Ui::Widget)
  9. {
  10. ui->setupUi(this);
  11. }
  12. Widget::~Widget()
  13. {
  14. delete ui;
  15. }
  16. void Widget::on_leAppId_editingFinished()
  17. {
  18. appid = ui->leAppId->text();
  19. qDebug() << "appid = " << appid;
  20. }
  21. void Widget::on_leKey_editingFinished()
  22. {
  23. key = ui->leKey->text();
  24. qDebug() << "key = " << key;
  25. }
  26. void Widget::on_pbEncryption_clicked()
  27. {
  28. if(salt.isEmpty())
  29. {
  30. quint32 randNumber = QRandomGenerator::global()->bounded(1073741825, 2147483648);
  31. //根据指定的基数返回与number相当的字符串
  32. salt = QString::number(randNumber,10);
  33. ui->leSalt->setText(salt);
  34. qDebug() << "randNumber=" << randNumber;
  35. }
  36. qDebug() << "salt=" << salt;
  37. dictionary = new electronicDictionary(0,appid,key,salt);
  38. dictionary->show();
  39. this->hide();
  40. }
  41. void Widget::on_leSalt_editingFinished()
  42. {
  43. salt = ui->leSalt->text();
  44. qDebug() << "salt=" << salt;
  45. }

main.cpp

  1. #include "widget.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. Widget w;
  7. w.show();
  8. return a.exec();
  9. }

以上为整个项目粗略概述,有疑问或有意见、建议的恳请下方留言。

源码包地址:https://download.csdn.net/download/ezio_bel/11973260

优衣酱有时间再进行具体的阐述和改进,可能当作插叙放到下次博客中也可能放到下下次。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/299670
推荐阅读
相关标签
  

闽ICP备14008679号