How to run a function in another thread
-
I tried to run a function in the event loop of another thread by clicking a QPushButton but failed.
The sources are as follows:
// main.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include "workthread.h" #include <QApplication> #include <QObject> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); WorkThread t; QObject::connect(w.ui->pushButton, &QPushButton::clicked, &t, &WorkThread::printThreadId, Qt::QueuedConnection); t.start(); return a.exec(); }
// mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
// mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include "workthread.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); qDebug() << "MainWindow thread:" << QThread::currentThreadId(); } MainWindow::~MainWindow() { delete ui; }
// workthread.h #ifndef WORKTHREAD_H #define WORKTHREAD_H #include <QThread> class MainWindow; class WorkThread : public QThread { Q_OBJECT public: WorkThread(); public slots: void printThreadId(); protected: void run(); }; #endif // WORKTHREAD_H
// workthread.cpp #include "workthread.h" #include <QDebug> WorkThread::WorkThread() {} void WorkThread::printThreadId() { qDebug() << "WorkThread slot thread:" << QThread::currentThreadId(); } void WorkThread::run() { qDebug() << "WorkThread thread:" << QThread::currentThreadId(); exec(); }
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>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>150</x> <y>110</y> <width>99</width> <height>27</height> </rect> </property> <property name="text"> <string>PushButton</string> </property> </widget> </widget> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
The output of the program is like:
MainWindow thread: 0x7ff21ff9d780
WorkThread thread: 0x7ff1e7fff700
WorkThread slot thread: 0x7ff21ff9d780
WorkThread slot thread: 0x7ff21ff9d780from which we can conclude that the method invoked by the
clicked
signal of pushButton is run in the main thread.So how can I run this function in my workthread after pressing the button?
-
@Disco thats what you get for choosing the subclassing thread approach
Anyway I leave this repo link here:
https://github.com/DeiVadder/QtThreadExample
I would almost always suggest the worker approach, but if it's a fire and forget function you want to run in parallel, QThread::create it probably the way to go.