Qt5 Tutorial QThreads - Gui Thread - 2020
In this tutorial, we will learn communication between threads and GUI control.
In this example, when we run our code, we get the following dialog:
A new thread will be created in the constructor of the dialog.
Hitting the "Start" button will trigger slot, and in that slot, start() method of the thread will be called. The start() will call the thread's run() method where a valueChanged() signal will be emitted. The valueChanged() signal is connected to the onValueChanged() which will update the count label on the dialog box.
We'll use Qt Gui Application with QDialog.
Then, create MyThread class:
Here are the codes:
No changes made to main.cpp
#include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); }
dialog.h
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QString> #include "mythread.h" namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); MyThread *mThread; private: Ui::Dialog *ui; public slots: void onValueChanged(int); private slots: // for Start button void on_pushButton_clicked(); // for Stop button void on_pushButton_2_clicked(); }; #endif // DIALOG_H
dialog.cpp
#include "dialog.h" #include "ui_dialog.h" #include "mythread.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); // create an instance of MyThread mThread = new MyThread(this); // connect signal/slot connect(mThread, SIGNAL(valueChanged(int)), this, SLOT(onValueChanged(int))); } Dialog::~Dialog() { delete ui; } // Absorb the signal emitted from a run() method // and reflect the count change to the count label // in our dialog void Dialog::onValueChanged(int count) { ui->label->setText(QString::number(count)); } // Start button void Dialog::on_pushButton_clicked() { mThread->start(); } // Stop button void Dialog::on_pushButton_2_clicked() { mThread->Stop = true; }
mythread.h
#ifndef MYTHREAD_H #define MYTHREAD_H #include <QThread> class MyThread : public QThread { Q_OBJECT public: explicit MyThread(QObject *parent = 0, bool b = false); void run(); // if Stop = true, the thread will break // out of the loop, and will be disposed bool Stop; signals: // To communicate with Gui Thread // we need to emit a signal void valueChanged(int); public slots: }; #endif // MYTHREAD_H
mythread.cpp
#include "mythread.h" #include <QDebug> MyThread::MyThread(QObject *parent, bool b) : QThread(parent), Stop(b) { } // run() will be called when a thread starts void MyThread::run() { for(int i = 0; i <= 100; i++) { QMutex mutex; // prevent other threads from changing the "Stop" value mutex.lock(); if(this->Stop) break; mutex.unlock(); // emit the signal for the count label emit valueChanged(i); // slowdown the count change, msec this->msleep(500); } }
Qt 5 Tutorial
- Hello World
- Signals and Slots
- Q_OBJECT Macro
- MainWindow and Action
- MainWindow and ImageViewer using Designer A
- MainWindow and ImageViewer using Designer B
- Layouts
- Layouts without Designer
- Grid Layouts
- Splitter
- QDir
- QFile (Basic)
- Resource Files (.qrc)
- QComboBox
- QListWidget
- QTreeWidget
- QAction and Icon Resources
- QStatusBar
- QMessageBox
- QTimer
- QList
- QListIterator
- QMutableListIterator
- QLinkedList
- QMap
- QHash
- QStringList
- QTextStream
- QMimeType and QMimeDatabase
- QFile (Serialization I)
- QFile (Serialization II - Class)
- Tool Tips in HTML Style and with Resource Images
- QPainter
- QBrush and QRect
- QPainterPath and QPolygon
- QPen and Cap Style
- QBrush and QGradient
- QPainter and Transformations
- QGraphicsView and QGraphicsScene
- Customizing Items by inheriting QGraphicsItem
- QGraphicsView Animation
- FFmpeg Converter using QProcess
- QProgress Dialog - Modal and Modeless
- QVariant and QMetaType
- QtXML - Writing to a file
- QtXML - QtXML DOM Reading
- QThreads - Introduction
- QThreads - Creating Threads
- Creating QThreads using QtConcurrent
- QThreads - Priority
- QThreads - QMutex
- QThreads - GuiThread
- QtConcurrent QProgressDialog with QFutureWatcher
- QSemaphores - Producer and Consumer
- QThreads - wait()
- MVC - ModelView with QListView and QStringListModel
- MVC - ModelView with QTreeView and QDirModel
- MVC - ModelView with QTreeView and QFileSystemModel
- MVC - ModelView with QTableView and QItemDelegate
- QHttp - Downloading Files
- QNetworkAccessManager and QNetworkRequest - Downloading Files
- Qt's Network Download Example - Reconstructed
- QNetworkAccessManager - Downloading Files with UI and QProgressDialog
- QUdpSocket
- QTcpSocket
- QTcpSocket with Signals and Slots
- QTcpServer - Client and Server
- QTcpServer - Loopback Dialog
- QTcpServer - Client and Server using MultiThreading
- QTcpServer - Client and Server using QThreadPool
- Asynchronous QTcpServer - Client and Server using QThreadPool
- Qt Quick2 QML Animation - A
- Qt Quick2 QML Animation - B
- Short note on Ubuntu Install
- OpenGL with QT5
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part A
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part B
- Video Player with HTML5 QWebView and FFmpeg Converter
- Qt5 Add-in and Visual Studio 2012
- Qt5.3 Installation on Ubuntu 14.04
- Qt5.5 Installation on Ubuntu 14.04
- Short note on deploying to Windows
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization