Answer the question
In order to leave comments, you need to log in
QThread is slow on macOS. What is the reason?
Code example:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Worker *worker = new Worker();
QThread *thread = new QThread;
worker->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), observer, SLOT(doWork()));
QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
QObject::connect(worker, SIGNAL(finished()), worker,
SLOT(deleteLater()));
QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start(QThread::HighestPriority);
return a.exec();
}
void Worker::doWork() {
while (!is_stop) {
qDebug() << count;
count++;
thread()->msleep(1000);
}
emit finished();
}
Answer the question
In order to leave comments, you need to log in
Try using a timer with QEventLoop instead of msleep.
It's better to include QThread::finished() rather than worker in deleteLater().
Yes, and a.exec(); by itself will not wait for those deletion signals, you must also thread->wait() after a.exec() and then return 0;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question