moveToThread 相当于是一个多线程的阻塞函数,
本案例可多次点击按钮,多次触发,这个信号触发类似于内部建立一个队列,处理函数会按照顺序处理信号
test_moveToThread.pro
#------------------------------------------------- # # Project created by QtCreator 2023-03-23T11:59:07 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = test_moveToThread TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QDebug> #include <QThread> class WorkThread: public QObject { Q_OBJECT signals: void sigDowork(QString info); public slots: void doWork(QString info){ qDebug() << "线程id 1:" << QThread::currentThreadId(); emit sigDowork(info); } }; namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); signals: void sigSendInfo(QString info); void sigQthreadShowUiInfo(QString key="",QString info=""); private: //void showInfo(QString info){} private slots: void testqthread(QString info); void ShowUiInfo(QString key="",QString info=""); private: Ui::MainWindow *ui; WorkThread* worker; QThread* qthread; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); worker = new WorkThread(); qthread = new QThread(this); worker->moveToThread(qthread); connect(ui->btn__f3, &QPushButton::clicked, this,[=](){ qDebug() << "线程id 2:" << QThread::currentThreadId(); ShowUiInfo("debug","put task"); emit sigSendInfo("test sigSendInfo...."); });//通过线程开启来触发定时器开启 connect(this, &MainWindow::sigSendInfo, worker,&WorkThread::doWork);//通过线程开启来触发定时器开启 connect(worker, &WorkThread::sigDowork, this, &MainWindow::testqthread, Qt::DirectConnection);//信号发出后立即调用槽函数,槽函数在发出信号的线程中执行 connect(qthread, &QThread::finished, qthread, &QObject::deleteLater);//线程结束,自动关闭线程 qthread->start(); connect(this, &MainWindow::sigQthreadShowUiInfo, this, &MainWindow::ShowUiInfo); } MainWindow::~MainWindow() { delete ui; } void MainWindow::testqthread(QString info) { //qDebug() << "线程id 3:" << QThread::currentThreadId(); //ui->data_reciveInfo->appendPlainText(info);//直接调用可能导致出错,可以使用全局变量,或者信号的方式进行数据显示 qDebug()<<"start...val:"<< info << "线程id 3:" << QThread::currentThreadId(); emit sigQthreadShowUiInfo("debug",info); emit sigQthreadShowUiInfo("debug","start worker"); QThread::sleep(5);//5秒 qDebug()<<"end ..." << "线程id 3:" << QThread::currentThreadId(); emit sigQthreadShowUiInfo("debug","stop worker"); } void MainWindow::ShowUiInfo(QString key, QString info) { ui->data_reciveInfo->appendPlainText(info); }
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>707</width> <height>430</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0" colspan="2"> <widget class="QPlainTextEdit" name="data_reciveInfo"/> </item> <item row="1" column="0"> <spacer name="horizontalSpacer"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>529</width> <height>20</height> </size> </property> </spacer> </item> <item row="1" column="1"> <widget class="QPushButton" name="btn__f3"> <property name="minimumSize"> <size> <width>151</width> <height>41</height> </size> </property> <property name="maximumSize"> <size> <width>201</width> <height>16777215</height> </size> </property> <property name="text"> <string>测试</string> </property> </widget> </item> </layout> </widget> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>707</width> <height>23</height> </rect> </property> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> <enum>TopToolBarArea</enum> </attribute> <attribute name="toolBarBreak"> <bool>false</bool> </attribute> </widget> <widget class="QStatusBar" name="statusBar"/> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
运行结果
标签:moveToThread,多线程,qt,deprecated,MainWindow,ui,include,mainwindow From: https://www.cnblogs.com/RYSBlog/p/17247188.html