#include #include #include #include #include #include #include class MyThread : public QThread { Q_OBJECT public: MyThread(QObject *parent) : QThread(parent) { } public slots: void emitSignal1() { mutex.lock(); signalQueue << 1; mutex.unlock(); } void emitSignal2() { mutex.lock(); signalQueue << 2; mutex.unlock(); } signals: void sig1(); void sig2(); protected: void run() { while (true) { mutex.lock(); unsigned char s = 0; if (!signalQueue.isEmpty()) s = signalQueue.dequeue(); mutex.unlock(); switch (s) { case 1: emit sig1(); break; case 2: emit sig2(); break; } } } QMutex mutex; QQueue signalQueue; }; class MyWindow : public QWidget { Q_OBJECT public: MyWindow() { MyThread* t = new MyThread(this); connect(t, SIGNAL(sig1()), this, SLOT(onSig1())); connect(t, SIGNAL(sig2()), this, SLOT(onSig2())); t->start(); // post event before the event loop starts t->emitSignal1(); // post event after the event loop has started QTimer::singleShot(0, t, SLOT(emitSignal2())); } public slots: void onSig1() { qDebug("--- onSig1()"); } void onSig2() { qDebug("--- onSig2()"); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MyWindow w; w.showMaximized(); return a.exec(); } #include "main.moc"