#include #include #include #include class test2 : public QObject { Q_OBJECT public: test2(QObject *parent) : QObject(parent) {} public slots: void testslot() { qDebug() << "testslot"; } }; class test : public QObject { Q_OBJECT public: test(test2 *d) : d(d) {} public slots: void start() { QTimer *timer = new QTimer(this); timer->setInterval(1); timer->setSingleShot(false); timer->start(); connect(timer, &QTimer::timeout, d, &test2::testslot, Qt::BlockingQueuedConnection); } private: test2 *d; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); test2 *t2 = new test2(&a); QThread *thread = new QThread(&a); test *t = new test(t2); t->moveToThread(thread); QObject::connect(thread, &QThread::started, t, &test::start); QObject::connect(thread, &QThread::finished, t, &test::deleteLater); thread->start(); QTimer::singleShot(5000, [thread]() { qDebug() << "quit"; thread->quit(); while (!thread->isFinished()) { qDebug() << "wait"; thread->wait(1000); } qDebug() << "app.exit"; qApp->exit(0); }); return a.exec(); } #include "main.moc"