赞
踩
基于百度api的在线词典是优衣酱结合百度API开发文档和网上教程JSON花了两天完成的,项目非常简陋,功能得益于百度翻译强大的翻译能力。现在发布到博客上,供大家交流学习。
说明:共包含两个界面:登录界面和翻译界面。
登录界面,需要输入appid和密匙,基于开发文档要求,要通过salt来获得签名,界面上salt一栏可以自定义输入0到2147483648任意的数值,也可以留空让程序自己生成32位二进制转十进制的随机数。
翻译界面,编辑发送区,点击翻译按钮,可以将翻译文本回显在接收区,默认中文转英文,可以自定义转换方式,但是必须遵照支持文档的要求来。
- #ifndef ELECTRONICDICTIONARY_H
- #define ELECTRONICDICTIONARY_H
-
- #include <QWidget>
-
- #include <QNetworkRequest> //http网址管理类
- #include <QNetworkReply> //网页回复数据管理类
- #include <QNetworkAccessManager> //http请求方式管理类
-
- namespace Ui {
- class electronicDictionary;
- }
-
- class electronicDictionary : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit electronicDictionary(QWidget *parent = 0,QString appid = "", QString key = "" , QString salt = "");
- ~electronicDictionary();
-
- private slots:
- void on_teSend_textChanged();
-
- void on_pbTranslate_clicked();
-
- void httpReply(QNetworkReply *);
- void on_leFrom_editingFinished();
-
- void on_leTo_editingFinished();
-
- void on_pbSwitch_clicked();
-
- private:
- Ui::electronicDictionary *ui;
-
- QString url;
-
- QString q;
- QString from;
- QString to;
-
- QString salt;
- QString sign;
- QString signMd5;
- QString appid;
- QString key;
-
- QNetworkRequest *request;
- QNetworkAccessManager *manager;
-
- QString networkReply;
-
- QJsonObject stringToJson(const QString jsonString);
- void md5Encryption();
- void urlEncode();
- };
-
- #endif // ELECTRONICDICTIONARY_H
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include <QWidget>
- #include "electronicdictionary.h"
- namespace Ui {
- class Widget;
- }
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit Widget(QWidget *parent = 0);
- ~Widget();
-
- private slots:
- void on_leAppId_editingFinished();
- void on_leKey_editingFinished();
- void on_pbEncryption_clicked();
- void on_leSalt_editingFinished();
- private:
- Ui::Widget *ui;
-
- QString salt;
- QString appid;
- QString key;
-
- electronicDictionary * dictionary;
- };
-
- #endif // WIDGET_H
- #include "electronicdictionary.h"
- #include "ui_electronicdictionary.h"
- #include <QDebug>
- #include <QJsonDocument>
- #include <QJsonObject>
- #include <QJsonParseError>
- #include <QJsonArray>
- #include <QVariant>
- #include <QTextCodec>
- electronicDictionary::electronicDictionary(QWidget *parent,QString appid, QString key, QString salt):
- QWidget(parent),
- ui(new Ui::electronicDictionary)
- {
- ui->setupUi(this);
- //默认初始化
- from = "en";
- to = "zh";
- this->appid = appid;
- this->key = key;
- this->salt = salt;
- }
-
- electronicDictionary::~electronicDictionary()
- {
- delete ui;
- }
-
- void electronicDictionary::on_teSend_textChanged()
- {
- q = ui->teSend->toPlainText();
- }
-
- void electronicDictionary::on_pbTranslate_clicked()
- {
- md5Encryption();
- // urlEncode();
- //设置访问地址
- url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
- url += "q=" + q +
- "&from=" + from +
- "&to=" + to +
- "&appid=" + appid +
- "&salt=" + salt +
- "&sign=" + signMd5;
- qDebug() << "url=" << url;
-
- //实例化网址管理
- request = new QNetworkRequest;
- //1、设置具体的网址
- request->setUrl(url);
- //2、实例化http请求方式
- manager = new QNetworkAccessManager;
- //关联信号 finished, 当网页回复数据时触发,此时才能在槽函数中,读取请求到的数据
- connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(httpReply(QNetworkReply *)));
- //3、get方法请求(get方法上传)
- manager->get(*request);
- }
-
- void electronicDictionary::httpReply(QNetworkReply * reply)
- {
- networkReply = "";
- networkReply.append(reply->readAll());
- qDebug() << "networkReply" << networkReply;
-
- QJsonObject obj = stringToJson(networkReply);
- if(obj.contains("trans_result"))
- {
- QJsonValue val = obj.value("trans_result");
- if(val.isArray())
- {
- QJsonArray array = val.toArray();
- // qDebug() << array; //将该对象一次性打印出来
- qDebug() << array.at(0);
- if(array.at(0).isObject())
- {
- QJsonObject obj = array.at(0).toObject();
- if(obj.contains("dst"))
- {
- QString utf8Reply = obj.value("dst").toString();
- qDebug() << "utf8Reply" << utf8Reply;
- ui->teRecv->clear();
- ui->teRecv->insertPlainText(utf8Reply);
- }
- }
- }else{
- qDebug() << "错误:"<<val;
- }
- }
- }
-
- QJsonObject electronicDictionary::stringToJson(const QString jsonString)
- {
- //QTextCodec类提供文本编码之间的转换
- //codecForName():搜索所有已安装的qtextcodec对象,并返回与名称最匹配的对象;
- //匹配是不区分大小写的。如果找不到与名称匹配的编解码器,则返回0。
- QTextCodec *tc = QTextCodec::codecForName("UTF-8");//防止中文乱码
- //指示编译器指定名称的参数不在函数正文中使用。这可用于抑制编译器警告,同时允许在其签名中使用有意义的参数名称定义函数。
- Q_UNUSED(tc);
-
- QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonString.toUtf8());
- if (jsonDocument.isNull()){
- qDebug() << "数据解析有误"; //判断是否解析出问题
- }
- QJsonObject jsonObject = jsonDocument.object();
- // qDebug() << "jsonObject" << jsonObject;
- return jsonObject;
- }
-
- void electronicDictionary::on_leFrom_editingFinished()
- {
- from = ui->leFrom->text();
- qDebug() << "from=" << from;
- }
-
- void electronicDictionary::on_leTo_editingFinished()
- {
- to = ui->leTo->text();
- qDebug() << "to=" << to;
- }
-
- void electronicDictionary::on_pbSwitch_clicked()
- {
- QString tmp = from;
- from = to;
- to = tmp;
-
- ui->leFrom->setText(from);
- ui->leTo->setText(to);
- qDebug() << "from=" << from << ":to=" << to;
- }
-
- void electronicDictionary::md5Encryption()
- {
- sign = appid+q+salt+key;
- qDebug() << "sign = " << sign;
- //md5加密
- //构造一个对象,该对象可根据指定method和之后添加的data,创建出加密散列。
- QCryptographicHash md(QCryptographicHash::Md5);
- md.addData(sign.toLatin1());
-
- signMd5 = "";
- //返回最后的散列值。
- //返回字节数组的十六进制编码副本。十六进制编码使用数字0-9和字母a-f。
- //将字节数组附加到此字符串。
- signMd5.append(md.result().toHex());
- ui->leMd5->setText(signMd5);
- qDebug() << "signMd5=" << signMd5;
- }
-
- void electronicDictionary::urlEncode()
- {
- QTextCodec *tc = QTextCodec::codecForName("UTF-8");
- Q_UNUSED(tc);
- QByteArray byteArrayUTF8 = tc->fromUnicode(q);
- q.clear();
- q = QString(byteArrayUTF8.toPercentEncoding());
- qDebug() << "URL encode q=" << q;
- }
-
-
- #include "widget.h"
- #include "ui_widget.h"
- #include <QDebug>
- #include <QRandomGenerator>
- #include <QCryptographicHash>
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::on_leAppId_editingFinished()
- {
- appid = ui->leAppId->text();
- qDebug() << "appid = " << appid;
- }
-
- void Widget::on_leKey_editingFinished()
- {
- key = ui->leKey->text();
- qDebug() << "key = " << key;
- }
-
- void Widget::on_pbEncryption_clicked()
- {
- if(salt.isEmpty())
- {
- quint32 randNumber = QRandomGenerator::global()->bounded(1073741825, 2147483648);
- //根据指定的基数返回与number相当的字符串
- salt = QString::number(randNumber,10);
- ui->leSalt->setText(salt);
- qDebug() << "randNumber=" << randNumber;
- }
- qDebug() << "salt=" << salt;
-
- dictionary = new electronicDictionary(0,appid,key,salt);
- dictionary->show();
- this->hide();
- }
-
- void Widget::on_leSalt_editingFinished()
- {
- salt = ui->leSalt->text();
- qDebug() << "salt=" << salt;
- }
- #include "widget.h"
- #include <QApplication>
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
-
- return a.exec();
- }
以上为整个项目粗略概述,有疑问或有意见、建议的恳请下方留言。
源码包地址:https://download.csdn.net/download/ezio_bel/11973260
优衣酱有时间再进行具体的阐述和改进,可能当作插叙放到下次博客中也可能放到下下次。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。