Last active
September 23, 2021 14:14
-
-
Save lamprosg/4581356 to your computer and use it in GitHub Desktop.
Qt threading - Basic use of QThread.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <QThread> | |
| #include "mythread.h" | |
| int main(int argc, char *argv[]) | |
| { | |
| QCoreApplication a(argc, argv); | |
| QThread thread; //Thread object | |
| MyThread cObject; //My class object | |
| //Set it up | |
| cObject.DoSetup(thread); | |
| //This is where the real work happens | |
| //Take the cObject and move it to the thread (So we don't have to sublass it) | |
| cObject.moveToThread(&thread); //Takes a pointer so we add the address of the thread | |
| //Start the thread | |
| thread.start(); | |
| /**********************************************************/ | |
| //You can set a priority for the thread. Ex: | |
| thread.start(QThread::HighestPriority); //or LowestPriority | |
| /**********************************************************/ | |
| return a.exec(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "mythread.h" | |
| MyThread::MyThread(QObject *parent) : | |
| QObject(parent) | |
| { | |
| } | |
| void MyThread::DoSetup(QThread &cThread) | |
| { | |
| connect(&cThread,SIGNAL(started()),this,SLOT(DoWork())); | |
| } | |
| void MyThread::DoWork() | |
| { | |
| //Once the thread is started, all the work will be done here | |
| /**********************************************************/ | |
| //Preventing deadlocks in threads when accessing global variables | |
| //You need to allow only 1 thread to change the global variable by locking the access | |
| //Ex: | |
| QMutex mutex; | |
| mutex.lock(); | |
| //....Update your variable(s) | |
| mutex.unlock(); | |
| /**********************************************************/ | |
| /**********************************************************/ | |
| //Slowing the thread down. For UI friendly reasons or for CPU reasons. | |
| this->sleep(2); //in secods | |
| this-<msleep(100); //in milliseconds | |
| /**********************************************************/ | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Create a class (ex. myThread) and subclass it to QObject | |
| #include <QObject> | |
| #include <QDebug> | |
| #include <QThread> | |
| class MyThread : public QObject | |
| { | |
| Q_OBJECT | |
| public: | |
| explicit MyThread(QObject *parent = 0); | |
| //This passes a reference to the thread and will connect signals and slots | |
| //When the thread is started, DoWork will be emmited. | |
| void DoSetup(QThread &cThread); | |
| signals: | |
| public slots: | |
| // | |
| void DoWork(); | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment