Qt使用线程以及线程同步
把信号连接过来的事件,放到线程中的槽函数处理,达到该槽函数的处理不会卡主线程的效果
例子如下:
class Dummy : public QObject { Q_OBJECT public: Dummy(QObject* parent = 0) : QObject(parent){} public slots: void emitsig() {emit sig();} signals: void sig(); }; /////////////////////////////////////////////////////////////////////////////////////// class Object : public QObject { Q_OBJECT public : Object(){} public slots: void slot(){qDebug() << "from thread slot:" << QThread::currentThreadId();} }; /////////////////////////////////////////////////////////////////////////////////////// include "main.moc" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug() << "main thread:" << QThread::currentThreadId(); QThread thread; Object obj; Dummy dummy; obj.moveToThread(&thread);//把槽函数操作移到子线程中处理 QObject::connect(&dummy, SIGNAL(sig()), &obj, SLOT(slot())); thread.start(); dummy.emitsig();//发送测试信号 return a.exec(); } // 输出: // main thread: 0x1a5c // from thread slot: 0x186c
线程的ID不同,表明slot在子线程中运行
具体可参考http://blog.csdn.net/c05170519/article/details/6459809