Simple multithreading with Qt: am I doing this right?

I am new to StackOverflow and am wondering if I am doing this correctly:

I am writing a simple Qt application for testing multithreading (I'm also a complete newbie). I created MainWindow, which contains widgets, and the MyThread class, which subclasses QThread and overrides the run () method.

The application simply displays two buttons: "Start Counter" and "Stop Counter" and a text field. When "start counter" is pressed, the workflow is created and runs in the background, continuously increasing the counter in the while loop and signaling to the main thread (where the graphical user interface is) with the updated value. When the “Stop counter” is pressed, the signal is sent to the main thread, which stops the while loop, and the counter stops until the “Start counter” is pressed again.

This works great ... but is this the best way? I am new to this and read a lot of people saying “not subclasses of QThread”, and other people say “subclassing QThread” and this is a bit confusing. If this is not the best way to implement such things (start a loop with intensive calculation in the background thread with the "start" and "stop" buttons), what is it? If I do it wrong, how do I do it right? I do not want to study wrong.

Thanks! And here is the code:

Mythread.h

#ifndef MYTHREAD_H #define MYTHREAD_H #include <QThread> #include <QMutex> class MyThread : public QThread { Q_OBJECT public slots: void stopRunning(); protected: virtual void run(); signals: void signalValueUpdated(QString); private: bool isRunning; }; 

Mythread.cpp

 #include "MyThread.h" #include <QString> void MyThread::run() { qDebug("Thread id inside run %d",(int)QThread::currentThreadId()); static int value=0; //If this is not static, then it is reset to 0 every time this function is called. isRunning = 1; while(isRunning == 1) { QString string = QString("value: %1").arg(value++); sleep(1/1000); //If this isn't here, the counter increments way too fast and dies, or something; the app freezes, anyway. emit signalValueUpdated(string); } } void MyThread::stopRunning() { isRunning = 0; } 

mainwindow.h

 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QApplication> #include <QPushButton> #include <QHBoxLayout> #include <QLineEdit> #include "MyThread.h" class MainWindow : public QWidget { Q_OBJECT public: MainWindow(QWidget *parent = 0); private: //Widgets QHBoxLayout * boxLayout; QPushButton * startButton; QPushButton * stopButton; QLineEdit * lineEdit; MyThread thread; }; #endif 

mainwindow.cpp

 #include "MainWindow.h" MainWindow::MainWindow(QWidget *parent) : QWidget(parent) { boxLayout = new QHBoxLayout(this); startButton = new QPushButton("Start Counter", this); stopButton = new QPushButton("Stop Counter", this); lineEdit = new QLineEdit(this); boxLayout->addWidget(startButton); boxLayout->addWidget(stopButton); boxLayout->addWidget(lineEdit); qDebug("Thread id %d",(int)QThread::currentThreadId()); //When the start button is pressed, invoke the start() method in the counter thread QObject::connect(startButton,SIGNAL(clicked()),&thread,SLOT(start()), Qt::QueuedConnection); //When the stop button is pressed, invoke the stop() method in the counter thread QObject::connect(stopButton,SIGNAL(clicked()),&thread,SLOT(stopRunning()), Qt::QueuedConnection); //When the counter thread emits a signal saying its value has been updated, reflect that change in the lineEdit field. QObject::connect(&thread,SIGNAL(signalValueUpdated(const QString&)),lineEdit,SLOT(setText(const QString&)), Qt::QueuedConnection); } 
+8
c ++ multithreading qt4 qthread
source share
2 answers

In most cases, subclassing QThread is the wrong way to execute threads in Qt. I suggest you read an article on threads, event loops, and others that can give you an idea of ​​how best to use threads in Qt, but don’t listen to anyone who claims that there is only one correct way to use QThread. There are two ways, and while subclassing is not needed at all, it can sometimes be useful. You just need to use the method without a subclass until you need a subclass. In your particular case, you do not need subclasses.

+4
source share

Replace sleep(1/1000); on msleep(100); Everything will be fine:)

+1
source share

All Articles