欢迎光临
我们一直在努力

QT第7个程序 - 成语填空

#include "chengyu.h"
#include<QRandomGenerator>

ChengYu::ChengYu()
{

}
QString ChengYu::MakeRndQue(int num)
{
QList<int> t;
for(int i=0;i<num;)
{
int n=QRandomGenerator::global()->bounded(0,4);
if(t.contains(n))
continue;
t.append(n);
i+=1;
}
QString result=Cy;
LastIndex.clear();
LastZi.clear();
foreach (int x, t) {
LastIndex.append(x);
LastZi.append(Cy[x]);
result.replace(Cy[x],"_");
}
LastTK=result;
return result;
}

#ifndef CHENGYU_H
#define CHENGYU_H

#include <QObject>
#include<QList>
#include<QChar>
class ChengYu
{
public:
explicit ChengYu();
QString Py;
QString Cy;
QString MakeRndQue(int num=1);
QString LastTK;
QList<int> LastIndex;
QList<QChar> LastZi;
signals:

};

#endif // CHENGYU_H

#include<QGuiApplication>
#include<QScreen>
#include "httphelper.h"
#include"chengyu.h"
#include<QDebug>
#include"mainwindow.h"
#include <QMainWindow>
#include<QPushButton>
#include<QLabel>
#include<QComboBox>
#include<QGroupBox>
#include<QCheckBox>
#include<QRandomGenerator>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
Count=0;
NowCount=0;
OKCount=0;
setFixedSize(800,500);
this->move(QGuiApplication::primaryScreen()->size().width()/2-this->width()/2,
QGuiApplication::primaryScreen()->size().height()/2-this->height()/2);
QPushButton * btn = new QPushButton("更新数据",this);
btn->resize(100,30);
btn->move(10,10);
btn->setFont(QFont("黑体",12));
QLabel * laState = new QLabel("",this);
laState->resize(200,30);
laState->move(this->width()-20-laState->width(),10);
laState->setFont(QFont("黑体",12));
QLabel * la1 = new QLabel("填空字数:",this);
la1->resize(100,30);
la1->move(10,40);
la1->setFont(QFont("黑体",12));
QComboBox *cmbt = new QComboBox(this);
cmbt->addItems({"1","2"});
cmbt->resize(100,30);
cmbt->move(120,40);
cmbt->setFont(QFont("黑体",12));

QLabel * la2 = new QLabel("成语个数:",this);
la2->resize(100,30);
la2->move(230,40);
la2->setFont(QFont("黑体",12));
QComboBox *cmbg = new QComboBox(this);
cmbg->addItems({"10","20","30","40","50","100"});
cmbg->resize(100,30);
cmbg->move(340,40);
cmbg->setFont(QFont("黑体",12));
QCheckBox *chkpy = new QCheckBox("显示拼音",this);
chkpy->resize(100,30);
chkpy->move(450,40);
chkpy->setFont(QFont("黑体",12));
chkpy->setChecked(true);
QLabel * la3 = new QLabel("",this);
la3->resize(this->width()-20-(chkpy->x()+chkpy->width()),30);
la3->move(this->width()-10-la3->width(),40);
la3->setFont(QFont("黑体",12));
la3->setStyleSheet("QLabel{border:1px solid skyblue;}");
laState->resize(la3->size());
laState->move(la3->x(),10);

QGroupBox *grp=new QGroupBox("成语填空",this);\\
grp->resize(this->width()-20,this->height()-90);
grp->move(10,80);
grp->setFont(QFont("黑体",12));

QLabel * la4 = new QLabel("常用成语",grp);
la4->resize(grp->width()-20-100,60);
la4->move(10,10);
la4->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
la4->setFont(QFont("黑体",30));

int wi=(grp->width()-20)/5;
int hi=(grp->height()-70)/4;
int Index=1;
int Top=70;
for(int i=0;i<5;i++)
{
for(int j=0;j<4;j++)
{
QPushButton *b1 = new QPushButton("",grp);
b1->resize(wi-10,hi-10);
b1->move(10+i*wi+5,Top+j*hi+5);
b1->setProperty("Name",QString::number(Index++));
b1->setStyleSheet("QPushButton{color:blank}");
b1->setFont(QFont("黑体",20));
connect(b1,&QPushButton::clicked,this,[=](){

if(b1->property("Result")==b1->text())
{
if(isCheck)
{
OKCount+=1;
}
b1->setStyleSheet("QPushButton{color:blue}");
}
else
{
isCheck=true;
b1->setStyleSheet("QPushButton{color:red}");
}
}
);
}
}
QPushButton *bx = new QPushButton("下一个",grp);
bx->resize(85,40);
bx->move(la4->x()+la4->width()+10,la4->y()+10);
bx->setFont(QFont("黑体",11));
connect(bx,&QPushButton::clicked,this,[=](){
Count=cmbg->currentText().toInt();
NowCount+=1;
if(NowCount>=Count)
{
//完成了,计算成绩
qDebug()<< OKCount<<"/"<<Count<<"成绩:"<<100.00*OKCount/Count<<"分";
Count=0;
NowCount=0;
OKCount=0;
return;
}
if(ML.length()<=0)
{
laState->setText("没有成语,请点[更新数据]");
return;
}
else
{
laState->setText(QString::number(NowCount));
}
int n=QRandomGenerator::global()->bounded(0,ML.length());
ChengYu ch=ML[n];
la4->setText(ch.MakeRndQue(cmbt->currentText().toInt()));
if(chkpy->checkState()==Qt::Checked)
{
la3->setText(ch.Py);
}
else
{
la3->setText("");
}
int ziindex=0;

QList<QChar> cs;
for(int i=0;i<20-ch.LastZi.count();i++)
{
cs.append(ML[QRandomGenerator::global()->bounded(0,ML.length())].Cy[QRandomGenerator::global()->bounded(0,4)]);
}
for(int i=0;i<ch.LastZi.count();i++)
{
cs.insert(QRandomGenerator::global()->bounded(0,cs.count()),ch.LastZi[i]);
}
foreach (QPushButton *btn, grp->findChildren<QPushButton *>())
{
if(btn->property("Name").toString()=="")
continue;
if(ch.LastZi.contains(cs[ziindex]))
{
btn->setProperty("Result",cs[ziindex]);
}
else
{
btn->setProperty("Result","");
}
btn->setText(cs[ziindex]);
btn->setStyleSheet("QPushButton{color:blank}");
ziindex+=1;

}
isCheck=false;
}
);
connect(chkpy,&QCheckBox::stateChanged,this,[=](){

la3->setVisible(chkpy->checkState()==Qt::Checked);
});
connect(btn,&QPushButton::clicked,this,[=](){
btn->setEnabled(false);
HttpHelper h;
QString ql="abcdefghijklmnopqrstuvwxyz";
QString html="";
ML.clear();
for(int i=0;i<ql.length();i++)
{
laState->setText("正在更新与("+ql[i]+")有关的成语……");
html=h.GetUrlData("https://chengyu.yw11.com/szm_"+ql[i]+".html");
//qDebug() << html;
int start=-1;
int end=-1;
start=html.indexOf("<a href=\\"/cy/");
while(start>0)
{
end=html.indexOf("</a>",start);
if(end>start)
{
ChengYu cy=GetChengYu(html.mid(start,end+4-start));
if(cy.Cy!="")
ML.append(cy);
start=end+4;
start=html.indexOf("<a href=\\"/cy/",start);
//qDebug() << cy.MakeRndQue(2);
continue;
}
break;
}
}
laState->setText("完成!成语数量:"+QString::number( ML.length()));
btn->setEnabled(true);
});
setWindowTitle("第7个程序 - 成语填空");
}

ChengYu MainWindow::GetChengYu(QString html)
{
ChengYu cy;
cy.Cy=html.mid(html.length()-8,4);
cy.Py=GetText(html,"span");
return cy;
}
QString MainWindow::GetAttText(QString one,QString obj,QString a)
{
int s=-1;
int e=-1;
s=one.indexOf("<"+obj);
if(s>=0)
{ s=one.indexOf(a+"=\\"",s+1);
if(s>0)
{
s+=a.length()+2;
e=one.indexOf("\\"",s+1);
if(e>s)
{
return one.mid(s+1,e-s-1);
}
}
}
return "";
}
QString MainWindow::GetText(QString one,QString obj)
{
int s=-1;
int e=-1;
s=one.indexOf("<"+obj);
if(s>=0)
{ s=one.indexOf(">",s+1);
if(s>0)
{
e=one.indexOf("<",s+1);
if(e>s)
{
return one.mid(s+1,e-s-1);
}
}
}
return "";
}
QString MainWindow::GetHtmlText(QString one,QString skey,QString ekey)//="<div class=\\"contson\\">"="</div>"
{
int s=one.indexOf(skey);
if(s>0)
{
int e=one.indexOf(ekey,s+skey.length()+1);
if(e>s)
{
return one.mid(s,e+ekey.length()-s-1);
}
}
return "";
}

MainWindow::~MainWindow()
{
}

#include "httphelper.h"
#include<QImage>
#include <QtNetwork>
#include<QNetworkAccessManager>
#include<QNetworkReply>
#include<QNetworkRequest>
#include<QDebug>
#include<QList>
#include<QSslError>

HttpHelper::HttpHelper(QObject *parent)
    : QObject{parent}
{

}
QImage HttpHelper::GetUrlImage(QString url)
{
   //QString baseUrl="https://www.ziqqq.com/zidian/?q=chuāng";
   QNetworkAccessManager *http =new QNetworkAccessManager(this);
    connect(http,&QNetworkAccessManager::finished,this,[=](QNetworkReply *re){
       // qDebug()<<"http finished!!";
        if(re->error())
        {
            qDebug()<<"http Error"<<re->errorString() << url;
        }
    });
    QUrl qq(url.replace("\\\\\\\\","//").replace("\\\\","/"));
  QNetworkRequest requ(qq);
  QSslConfiguration sslConfiguration(QSslConfiguration::defaultConfiguration());
  requ.setSslConfiguration(sslConfiguration);
  QNetworkReply *resp=http->get(requ);
  connect(resp,&QNetworkReply::finished,this,[=](){
     // qDebug()<<"respReady!";
  });
  connect(resp,&QNetworkReply::sslErrors,this,[=](const QList<QSslError> &ers){
     // qDebug() << ers;
  });
  while (!resp->isFinished() || !resp->isReadable()) {
      qApp->processEvents();//关键啊。
  }
  QByteArray backdata=resp->readAll();
  resp->disconnect();
  resp->close();
  resp->deleteLater();
  resp= nullptr;
  http->deleteResource(requ);
  http->clearAccessCache();
  http->clearConnectionCache();
  http->deleteLater();
  http=nullptr;
  return QImage::fromData(backdata);
}
QString HttpHelper::GetUrlData(QString url)
{
   //QString baseUrl="https://www.ziqqq.com/zidian/?q=chuāng";
   QNetworkAccessManager *http =new QNetworkAccessManager(this);
    connect(http,&QNetworkAccessManager::finished,this,[=](QNetworkReply *re){
      //  qDebug()<<"http finished!!";
        if(re->error())
        {
            qDebug()<<"http Error"<<re->errorString();
        }
    });
  QNetworkRequest requ(QUrl(url,QUrl::StrictMode));
  QSslConfiguration sslConfiguration(QSslConfiguration::defaultConfiguration());
  requ.setSslConfiguration(sslConfiguration);
  QNetworkReply *resp=http->get(requ);
  connect(resp,&QNetworkReply::finished,this,[=](){
    // qDebug()<<"respReady!";
  });
  connect(resp,&QNetworkReply::sslErrors,this,[=](const QList<QSslError> &ers){
     qDebug() << ers;
  });
  while (!resp->isFinished()) {
      qApp->processEvents();//关键啊。
  }
  QString backdata=resp->readAll();
  resp->disconnect();
  resp->close();
  resp->deleteLater();
  resp= nullptr;
  http->deleteResource(requ);
  http->clearAccessCache();
  http->clearConnectionCache();
  http->deleteLater();
  http=nullptr;
  return backdata;
}

赞(0)
未经允许不得转载:171主机测评 » QT第7个程序 - 成语填空
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址