一 编译结果
二 代码研究
2.1 全部类管理方式
enum class ShowType{
balloon,main};
enum class DictType{
ShanbayDict,YoudaoDict};
namespace DICT{
extern std::unique_ptr<Gui> gui;
extern std::unique_ptr<Application> app;
extern std::unique_ptr<ShanbayDict> shanbayDict;
extern std::unique_ptr<YoudaoDict> youdaoDict;
extern std::unique_ptr<Config> cfg;
extern Dictlogo* logo;
extern ShowType showType;
void init();
void queryWord(const QString& word);
void showWord(DictType dictType,const QString& wordinfo);
}
2.2 浮窗显示管理方式
void Application::run(){
DICT::init();
if(DICT::cfg->isStartloginshanbay()){
DICT::gui->showLoginWin();
}else{
if(!DICT::cfg->isAutohide()){
DICT::gui->showMainWin();
}
}
}
2.3 字典管理方式
QObject::connect(DICT::cfg.get(),&Config::signalChange,
[&](const QString& key, const QVariant& value){
if(key=="screentext"){
getscreenwordAction->setChecked(value.toBool());
}else if(key=="autospeak"){
autospeakAction->setChecked(value.toBool());
setScreenText();
}else if(key=="selectedtext"){
setScreenText();
}else if(key=="clipboardtext"){
setScreenText();
}
});
2.4 qml加载方式
//loginWin
void Gui::showLoginWin(){
qDebug()<<"show loginwin";
if(loginWin==nullptr){
qDebug()<<"load loginwin";
engine->load(QUrl(QStringLiteral("qrc:/qml/Login.qml")));
loginWin = qobject_cast<QWindow*>(engine->rootObjects().last());
loginWin->setIcon(QIcon(":/img/logo.png"));
//loginWin->setFlags(Qt::FramelessWindowHint);
//qDebug()<<engine.rootObjects().size()<<loginWin->objectName();
QObject::connect(loginWin,SIGNAL(signalRegisterClick()),
this,SLOT(registerClick()));
QObject::connect(loginWin, SIGNAL(signalLoginClick(QString,QString,QString)),
this, SIGNAL(signalLoginClick(QString,QString,QString)));
QObject::connect(loginWin,SIGNAL(signalFreshCaptchaImg()),
this,SIGNAL(signalFreshCaptchaImg()));
QObject::connect(this,SIGNAL(signalShowCaptchaImg(QVariant)),
loginWin,SLOT(showCaptchaImg(QVariant)));
QObject::connect(this,SIGNAL(signalSetLoginWinState(QVariant)),
loginWin,SLOT(setState(QVariant)));
}
loginWin->show();
}
2.5 网络通信管理方式
void ShanbayDict::httpfinished(QNetworkReply* reply){
if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString().isEmpty()){
qDebug()<<QString::fromUtf8(reply->readAll());
}
//qDebug()<<QString::fromUtf8(reply->readAll());
//qDebug()<<"Http request finished!"<<reply->error()<<reply->errorString()<<reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString()<<reply->url();
QJsonDocument jsonDoc;
QJsonObject jsonObj,vocObj,en_definitionsObj;
QString html;
QByteArray replData=reply->readAll();
sessionid=getCookie("csrftoken");
switch(httpAction){
case HttpAction::NoAction:
break;
case HttpAction::GetSessionidAction:
//sessionid=getCookie("csrftoken");//sessionid已经不在使用
//qDebug()<<"sessionid="<<sessionid<<replData;
//if(sessionidOk()){
this->state = NetState::login;
html=QString::fromUtf8(replData);
//qDebug()<<html;
captcha.parseHtml(html);
if(captcha.isneed()){
emit signalShowCaptcha();
}
//}
emit signalConnectFinished();
break;
case HttpAction::RefreshCaptchaImg:
html=QString::fromUtf8(replData);
//qDebug()<<html;
//captcha.parseHtml(html);
jsonDoc=QJsonDocument::fromJson(replData);
if(!jsonDoc.isNull()){
jsonObj=jsonDoc.object();
jsonObj=jsonObj.value("data").toObject();
captcha.id = jsonObj.value("key").toString();
captcha.url = jsonObj.value("image_url").toString();
emit signalShowCaptcha();
}
break;
case HttpAction::LoginAction:
httpAction=HttpAction::NoAction;
html=QString::fromUtf8(replData);
qDebug()<<html;
jsonDoc=QJsonDocument::fromJson(replData);
if(jsonDoc.isNull()){
nickname=QUrl::fromPercentEncoding(getCookie("username").toLatin1());
qDebug()<<"Login OK!nickname="<<nickname;
state=NetState::ok;
emit signalLoginFinished(true,"登录成功");
}else{
jsonObj = jsonDoc.object();
if(jsonObj.value("status_code").toInt()==0){
emit signalLoginFinished(true,"登录成功");
}else{
refreshCaptchaImg();
}
// qDebug()<<"Login failed!";
// //qDebug()<<html<<"验证码错误 pos:"<<html.indexOf("验证码错误");
// captcha.parseHtml(html);
// if(captcha.isneed()){
// emit signalShowCaptcha();
// }
// if(html.indexOf("请输入正确的用户名或注册邮箱和密码")>-1){
// emit signalLoginFinished(false,"请输入正确的用户名或注册邮箱和密码");
// }else{
// emit signalLoginFinished(false,"验证码错误");
// }
}
break;
case HttpAction::QueryWordAction:
//qDebug()<<"query word";
//jsondata=jsonParser->parse(reply->readAll(),&jsonok);
//jsonDoc=QJsonDocument::fromJson(replData);
emit signalRetWordinfo(QString::fromUtf8(replData));
// if(!jsonDoc.isNull()){
// // jsonObj=jsondata.toMap();
// jsonObj=jsonDoc.object();
// vocObj=jsonObj.value("voc").toObject();
// en_definitionsObj=vocObj.value("en_definitions").toObject();
// DICT::word->clear();
// DICT::word->name=vocObj.value("content").toString();
// DICT::word->learning_id=QString::number(jsonObj.value("learning_id").toDouble(),'g',15);
// DICT::word->definition=vocObj.value("definition").toString();
// DICT::word->pron=vocObj.value("pron").toString();
// DICT::word->audio=vocObj.value("audio").toString();
// //DICT::word->en_definitions="<br>";
// for(auto it=en_definitionsObj.constBegin();it!=en_definitionsObj.constEnd();it++){
// //qDebug()<<it.key()<<it.value();
// DICT::word->en_definitions+=QString("<br><font color='black'><b>%1. </b></font>").arg(it.key());
// if(it.value().isArray()){
// foreach(QJsonValue jv,it.value().toArray()){
// DICT::word->en_definitions+=QString("%1; ").arg(jv.toString());
// }
// }
// DICT::word->en_definitions+="<br>";
// }
// qDebug()<<QString("wordname=%1,learning_id=%2,definition=%3,pron=[%4],audio=%5,en_definition=%6")
// .arg(DICT::word->name)
// .arg(DICT::word->learning_id)
// .arg(DICT::word->definition)
// .arg(DICT::word->pron)
// .arg(DICT::word->audio)
// .arg(DICT::word->en_definitions);
// emit signalQueryFinished();
// //qDebug()<<DICT::word->learning_id;
// if(DICT::word->learning_id!="0"){
// queryExamples(DICT::word->learning_id);
// }
// //qDebug()<<jsonObj.value("voc").toMap().value("definition").toString();
// //qDebug()<<jsondata;
// }
break;
case HttpAction::AddExampleAction:
//qDebug()<<QString::fromUtf8(reply->readAll());
// jsondata=jsonParser->parse(reply->readAll(),&jsonok);
jsonDoc=QJsonDocument::fromJson(replData);
if(!jsonDoc.isNull()){
jsonObj=jsonDoc.object();
//qDebug()<<"example_status"<<jsonObj.value("example_status").toString();
double examplestatus=jsonObj.value("example_status").toDouble();
if(examplestatus==1){
emit signalAddExampleFinished("例句添加成功");
}else if(examplestatus==0){
emit signalAddExampleFinished("例句添加失败,例句或翻译不能为空");
}else if(examplestatus==100){
emit signalAddExampleFinished("例句添加失败,例句未包含指定原词,或者它的有效变体");
}else if(examplestatus==300){
emit signalAddExampleFinished("例句添加失败,例句及其译文总长超过300个字符");
}else{
emit signalAddExampleFinished("例句添加失败,指定词汇学习记录实例不存在,或者用户无权查看其内容");
}
}else{
emit signalAddExampleFinished("例句添加失败");
}
break;
case HttpAction::QueryWordExamplesAction:
qDebug()<<"query word examples";
jsonDoc=QJsonDocument::fromJson(replData);
if(!jsonDoc.isNull()){
jsonObj=jsonDoc.object();
if(jsonObj.value("examples_status").toDouble()==1){
foreach(QJsonValue jv,jsonObj.value("examples").toArray()){
//DICT::word->examples.append(jv.toObject());
}
// auto exmList=jsonObj.value("examples").toList();
// for(int i=0;i<exmList.size();i++){
// exmObj=exmList.at(i);
// DICT::word->examples.append(exmObj);
// qDebug()<<exmObj.value("first").toString()+exmObj.value("mid").toString()+exmObj.value("last").toString();
// qDebug()<<exmObj.value("translation").toString();
// }
emit signalQueryExampleFinished();
}
}
break;
case HttpAction::AddWordAction:
emit signalAddwordFinished(QString::fromUtf8(replData));
//qDebug()<<"add word_____________"<<QString::fromUtf8(reply->readAll());
// jsonDoc=QJsonDocument::fromJson(replData);
// if(!jsonDoc.isNull()){
// jsonObj=jsonDoc.object();
// //qDebug()<<"jsonObj"<<jsonObj;
// DICT::word->learning_id=QString::number(jsonObj.value("id").toDouble(),'g',15);
// qDebug()<<jsonObj.value("id").toDouble()<<"add word result learning id="<<DICT::word->learning_id<< (DICT::word->learning_id!="0");
// emit signalAddwordFinished(DICT::word->learning_id!="0");
// queryExamples(DICT::word->learning_id);
// }
//qDebug()<<QString::fromUtf8(reply->readAll());
break;
default:
// qDebug()<<reply->rawHeader("Content-Encoding");//gzip
// qDebug()<<QString::fromUtf8(reply->readAll());
break;
}
}
三 参考链接
lilydict: 百合词典,是一个开源跨平台桌面版词典软件,使用C++开发,图形界面框架库Qt5 QML。 (gitee.com)