Qt线程
- 线程创建方法
- 示例
线程创建方法
Qt下创建线程的方法有两种:
一种是通过继承QThread,并重写run()函数,在run()函数中,编写线程所做的事情,在需要线程的文件中,创建线程对象,并通过start()函数启动线程,运行run()函数。
另一种是继承QObject类之后,在此类中编写相应的信号和槽函数,在需要线程的类中,创建QThread类的指针和此类的指针,之后,通过函数movetoThread()函数实现线程的转移,关联信号槽后,同过QThread的指针调用start()函数开始线程。
示例
实现的功能是:在下面的界面上,程序运行起初,开始线程按钮和停止线程按钮都是置灰状态,无法使用,点击选择文件路径的按钮之后,弹出一个文件选择对话框,选择的文件路径显示在右侧的TextEdit控件中,此时开始线程按钮可以使用,点击开始线程按钮后,开始线程的按钮变为置灰,停止线程按钮变为可用,PlainTextEdit文本中开始显示所选文件的内容,并不断的显示其内容n遍,在这种状态下直接关闭对话框,即没有停止线程的情况下,关闭对话框,保证程序不奔溃,另一种,可点击停止线程按钮,关闭线程,PlainTextEdit中便不再继续输出文本,然后关闭对话框,程序正常结束。
界面如下图所示:
界面上对象与类的对应关系,如下图:
继承QObject的类mythread的代码:
mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H#include <QThread>
#include <QObject>
class QNetworkReply;class MyThread : public QObject
{Q_OBJECT
public:explicit MyThread(QObject *parent = nullptr);void SetNetworkReply(QNetworkReply *reply);
public slots:void replyFinished();
// void slotTransferNetReply(QNetworkReply *reply);
signals:void signDisplayString( QString str);private:QNetworkReply *m_reply;
};#endif // MYTHREAD_H
mythread.cpp
#include "mythread.h"
#include <QFile>
#include <QDebug>
#include <QThread>MyThread::MyThread(QObject *parent) : QObject(parent)
{m_Statua = false;m_bEndThread = false;
}
MyThread::~MyThread()
{m_Statua = false;m_bEndThread = false;
}void MyThread::slotGetFileContent()
{m_bEndThread = true;while(!m_Statua){QString strFileContent;strFileContent = GetFileContent();if(strFileContent.isEmpty()){return ;}emit signFileContent(strFileContent); QThread::sleep(1);//不加等待1s会出现线程读数据太快,界面还没来得及写,而导致崩溃}m_bEndThread = false;
}void MyThread::slotFilePath(const QString &strPath)
{m_strFilePath = strPath;
}void MyThread::StopThread()
{m_Statua = true;
}QString MyThread::GetFileContent()
{QString str;QFile file(m_strFilePath);if(!file.exists()){qDebug()<<QString("%1文件不存在").arg(m_strFilePath)<<endl;return str;}if(!file.open(QIODevice::ReadOnly|QIODevice::Text)){qDebug()<<m_strFilePath<<"文件打开失败"<<endl;return str;}str = file.readAll();file.close();//为文件打开后记得关闭return str;
}
界面类dialog的代码:
dialog.h
#ifndef DIALOG_H
#define DIALOG_H#include <QDialog>
#include <QThread>
#include "mythread.h"namespace Ui {
class Dialog;
}class Dialog : public QDialog
{Q_OBJECTpublic:explicit Dialog(QWidget *parent = nullptr);~Dialog();void closeEvent(QCloseEvent *event);
signals:void signFilePath(const QString &strPath);
public slots:void slotFileContent(const QString &str);
private slots:void on_pushButton_clicked();void on_pushButtonStart_clicked();void on_pushButtonStop_clicked();private:Ui::Dialog *ui;QThread *thread;MyThread *myThread;
// QThread thread;
// MyThread myThread;
};#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include <QFileDialog>
#include <QDebug>Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog)
{ui->setupUi(this);ui->pushButtonStop->setEnabled(false);ui->pushButtonStart->setEnabled(false);myThread = new MyThread;thread = new QThread(this);connect(ui->pushButtonStart,SIGNAL(clicked()),this,SLOT(on_pushButtonStart_clicked()));connect(ui->pushButtonStop,SIGNAL(clicked()),this,SLOT(on_pushButtonStop_clicked()));connect(this,&Dialog::signFilePath,myThread,&MyThread::slotFilePath);
// connect(this,&Dialog::signFilePath,&myThread,&MyThread::slotFilePath);// connect(&myThread,&MyThread::signFileContent,this,&Dialog::slotFileContent);
}Dialog::~Dialog()
{delete ui;
}void Dialog::closeEvent(QCloseEvent *event)//防止线程正在运行时,关闭进程导致的崩溃
{if(thread->isRunning()){myThread->StopThread();thread->quit();thread->wait();}
// if(thread.isRunning())
// {
// myThread.StopThread();// while(myThread.m_bEndThread)
// {
// qDebug()<<"-->closeEvent";
// QThread::sleep(1);
// }
// qDebug()<<"-->closeEvent thread end";
// thread.quit();
// thread.wait();
// }event->accept();
}void Dialog::slotFileContent(const QString &str)
{ui->plainTextEdit->appendPlainText(str);
}void Dialog::on_pushButton_clicked()
{QString strCurPath = QDir::currentPath();QString strFileter = QString("源文件(*.h *.cpp);;文本文件(*.txt);;所有文件(*.*)");//QString strFilePath = QFileDialog::getOpenFileName(this,tr("选择文件"),strCurPath,strFileter);if(strFilePath.isEmpty()){return ;}ui->textEdit->setText(strFilePath);emit signFilePath(strFilePath);ui->pushButton->setEnabled(false);ui->pushButtonStart->setEnabled(true);
}void Dialog::on_pushButtonStart_clicked()
{ui->pushButtonStop->setEnabled(true);ui->pushButtonStart->setEnabled(false);
// myThread = new MyThread;
// thread = new QThread(this);myThread->moveToThread(thread);//定义为指针就没有问题
// myThread.moveToThread(&thread);connect(thread,&QThread::started,myThread,&MyThread::slotGetFileContent);connect(thread,&QThread::finished,myThread,&QObject::deleteLater);connect(myThread,&MyThread::signFileContent,this,&Dialog::slotFileContent);thread->start();
// connect(&thread,&QThread::started,&myThread,&MyThread::slotGetFileContent);
// connect(&thread,&QThread::finished,&myThread,&QObject::deleteLater);
// connect(&myThread,&MyThread::signFileContent,this,&Dialog::slotFileContent);
// thread.start();
}void Dialog::on_pushButtonStop_clicked()
{if(thread->isRunning()){myThread->StopThread();thread->quit();//定义为变量会出现停止线程的时候没有停止,且加上quit()函数后会卡死,直接关闭对话框会崩溃,提示线程正在运行被关闭了thread->wait();ui->pushButton->setEnabled(true);ui->pushButtonStop->setEnabled(false);}
// if(thread.isRunning())
// {
// myThread.StopThread();
// while(myThread.m_bEndThread)
// {
// qDebug()<<"-->clos button";
// QThread::sleep(1);
// }
// qDebug()<<"-->clos button thread end";
// thread.exit();//定义为变量会出现停止线程的时候没有停止,且加上quit()函数后会卡死,直接关闭对话框会崩溃,提示线程正在运行被关闭了
// //thread.wait();
// ui->pushButton->setEnabled(true);
// ui->pushButtonStop->setEnabled(false);
// }
}
主函数main函数的代码没变:
#include "dialog.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Dialog w;w.show();return a.exec();
}
初次尝试,第一此定义的QThread对象和继承于QObject的类的对象,没有用指针,但是不知为啥,总是会程序奔溃,最后采用了指针,程序可以正常运行。求一份定义变量的情况下,此程序可以正常运行的指导,望各位同仁不吝赐教。