我是靠谱客的博主 羞涩小白菜,这篇文章主要介绍Qt 利用线程每秒获取网页数据并保存,现在分享给大家,希望可以做个参考。

所需模块

QT += core gui
QT += axcontainer
QT += network

头文件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <QObject> #include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkRequest> #include <QtNetwork/QNetworkReply> #include <QThread> #include <QUrl> #include <QMainWindow> #include <QString> #include <QDebug> #include <QFile> #include <QPushButton>

创建线程

新建MyThread.h

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <QObject> class MyThread : public QObject { Q_OBJECT public: explicit MyThread(QObject *parent = nullptr); void myTimeout(); void setFlag(bool flag = true); signals: void mySignal(); public slots: private: bool isStop; };

在生成的MyThread.cpp中添加线程响应

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "mythread.h" #include <QThread> #include<QDebug> MyThread::MyThread(QObject *parent) : QObject(parent) { isStop = false; } void MyThread::myTimeout() { while(isStop == false) { QThread::sleep(3);//强制暂停3秒,使得每三秒产生一次信号, emit mySignal();//每3秒发送一次信号给mainwindow.cpp qDebug()<<"子线程号"<<QThread::currentThread(); } } void MyThread::setFlag(bool flag) { isStop = flag;//定义标志位用于判断线程是否终止 }

在mainwindows.h中添加相应函数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void dealSignal();//处理子线程响应函数, void dealClose();//关闭线程响应 signals: void startThread();//开启线程信号 private slots: void finishedSlot(); private: Ui::MainWindow *ui; QPushButton pushButton_StartReading; MyThread *myT; QThread *thread; QNetworkAccessManager *m_NetManger; QNetworkReply *m_Reply; };

在.cpp文件中进行各个函数的攥写

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QThread> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //代码生成一个按钮,用于网络数据读取的触发方式 pushButton_StartReading.setParent(this); pushButton_StartReading.setText("startReading"); pushButton_StartReading.move(45,90); pushButton_StartReading.setMaximumSize(100,30); connect(&pushButton_StartReading,&QPushButton::pressed,this,&MainWindow::on_pushButton_StartReading); //网络获取数据部分 myT = new MyThread; thread = new QThread(this); myT->moveToThread(thread); connect(myT, &MyThread::mySignal, this, &MainWindow::dealSignal); qDebug()<<"主线程号"<<QThread::currentThread(); // connect(this, &MainWindow::startThread,myT, &MyThread::myTimeout); //当窗口销毁时,关闭线程 connect(this,&MainWindow::destroyed, this, &MainWindow::dealClose); } MainWindow::~MainWindow() { delete ui; } //网络信号响应 void MainWindow::dealSignal() { m_NetManger = new QNetworkAccessManager; QUrl url(https://www.bilibili.com/); m_Reply = m_NetManger->get(QNetworkRequest(url)); connect(m_Reply,SIGNAL(readyRead()),this,SLOT(finishedSlot())); } //网络信号结束响应 void MainWindow::dealClose() { if(thread->isRunning() == false)//判断线程是否终止 { return; } myT->setFlag(true); thread->quit(); thread->wait();//将等待线程完成本次响应操作后终止 delete myT; } void MainWindow::on_pushButton_StartReading() { if(thread->isRunning() == true)//判断此时线程是否正在运行,运行中则无视点击 { return ; } thread->start(); myT->setFlag(false); emit startThread();//启动线程 } void MainWindow::finishedSlot() { m_Reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); m_Reply->attribute(QNetworkRequest::RedirectionTargetAttribute); //ui->textBrowser->setText(string);可以添加ui控件观察内容 if(m_Reply->error() == QNetworkReply::NoError) { QByteArray bytes = m_Reply->readAll(); QString string =QString::fromUtf8(bytes); // QRegularExpression re(R"({[sS]*]})");//正则表达式,用于取出合适数据 //QRegularExpressionMatch match = re.match(string); //string = match.captured(); QFile file(../1.txt);//将网页内容保存为1.txt,保存在上层目录中 file.open(QIODevice::WriteOnly|QIODevice::Truncate); QTextStream out(&file); out.setCodec("UTF-8"); out<<string<<endl; file.close(); } else { qDebug()<<m_Reply->errorString(); } m_Reply->deleteLater(); } //可以通过ui控件添加pushButton_Stop按钮,建立槽函数触发dealclose() void MainWindow::on_pushButton_Stop_clicked() { dealClose(); }

最后

以上就是羞涩小白菜最近收集整理的关于Qt 利用线程每秒获取网页数据并保存的全部内容,更多相关Qt内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(163)

评论列表共有 0 条评论

立即
投稿
返回
顶部