I am making something in a Qt Widget Project, coded in C++. Because of what I need to do, I need infinite looping, and after doing my research, I realized that infinite looping in an object’s event doesn’t work, and instead, I need to use a some threading..
- I decided to use QTimer, but am thinking about using QThread. Which one should I use?
-
This is my QTimer code, which doesn’t seem to work:
Clock_Application::Clock_Application(QWidget *parent) : QMainWindow(parent), ui(new Ui::Clock_Application) { ui->setupUi(this); QTimer *timer_Stopwatch = new QTimer(this); connect(timer_Stopwatch, SIGNAL(timeout()), this, SLOT(timer_Start())); timer_Stopwatch->start(1000); }
Edit: a simple fix to this solution is using the QCoreApplication::processEvents() function, after every iteration of the loop, as that allows the widget application to process necessary events. This is a quick fix however, and using the QTimer library is a better way to fix it.
As you suggest yourself, performing your processing in a separate thread would be an option and I would surely look into doing that. I don’t think that the QTimer as you show it will help.
A quick fix however would be to manually call
QCoreApplication::processEvents()at the end of each loop. This will make sure that all the pending events get processed by Qt, keeping your UI responsive.