Does the output show "-x-x-x- Count Reached 10 -x-x-x-" if you wait long enough?
There is nothing wrong with your application, except that:
- there is a race condition between slots: worker->stopThread() is connected to both timer.stop() and worker->deleteLater(); worker could be deleted first, thus preventing timer.stop() from being run. Since the order in which the slots are run is unspecified in general, this may or may not happen in practice.
- the application never terminates. Nothing causes the event loop to exit.
I cannot see why stopping the timer would be necessary. However, you should probably exit the event loop cleanly. Here is a proposal that exits the program after the worker has finished its work:
int main(int argc, char *argv[])
{
Worker worker; // No need to allocate this on the heap; the stack is fine, and the object will be automatically destroyed at the end of the function (just like a and timer)
timer.start(1000);
return a.exec();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTimer timer;
Worker worker; // No need to allocate this on the heap; the stack is fine, and the object will be automatically destroyed at the end of the function (just like a and timer)
QCoreApplication::connect(&timer, SIGNAL(timeout()), &worker, SLOT(process()));
QCoreApplication::connect(&worker, SIGNAL(stopThread()), &a, SLOT(quit()));
timer.start(1000);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks