Qt5 Tutorial QThreads - Creating Threads - 2020
In this tutorial, we will learn how to create Threads.
As we already know, this is not a recommended way of using QThread not only because we're using lower level APIs for threads but also we may have a scaling issues later on. Qt recommends using QtConcurrent framework which has higher level of functional type APIs and we can even cancel, pause, or resume the results from a thread run.
However, we still need to know this level of APIs as well. In the next section of my Qt5 tutorial (Creating QThreads using QtConcurrent), we'll transform the code in this tutorial using QtConcurrent namespace.
Starting from Qt Console Application, we need to create MyThread class.
We make the MyThread class get inherited from QThread.
Let's look at main.cpp:
#include <QCoreApplication> #include "mythread.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MyThread thread1("A"), thread2("B"), thread3("C"); thread1.start(); thread2.start(); thread3.start(); return a.exec(); }
In the code, we creates three instances of MyThread class with QString names ("A", "B", and "C").
The void QThread::start(Priority priority = InheritPriority) slot begins execution of the thread by calling run() which we overrides in MyThread class.
OK. Let's look at other codes: mythread.cpp and mythread.h:
// mythread.h #ifndef MYTHREAD_H #define MYTHREAD_H #include <QThread> #include <QString> class MyThread : public QThread { public: // constructor // set name using initializer explicit MyThread(QString s); // overriding the QThread's run() method void run(); private: QString name; }; #endif // MYTHREAD_H // mythread.cpp #include "mythread.h" #include <QDebug> MyThread::MyThread(QString s) : name(s) { } // We overrides the QThread's run() method here // run() will be called when a thread starts // the code will be shared by all threads void MyThread::run() { for(int i = 0; i <= 100; i++) { qDebug() << this->name << " " << i; } }
We can see the three threads are running simultaneously accessing the same code segment (i.e. run()):
... "A" 87 "A" 88 "A" 89 "C" 91 "C" 92 "C" 93 "A" 90 "A" 91 "A" 92 "A" 93 "A" 94 "A" 95 "A" 96 "A" 97 "C" 94 "C" 95 "C" 96 "C" 97 "C" 98 "C" 99 "C" 100 "A" 98 "A" 99 "A" 100
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