/*!
* \file main.cpp
*
* Copyright (C) 2010, dbzhang800
* All rights reserved.
*
*/
#include <QtCore/QCoreApplication> 
#include <QtCore/QObject> 
#include <QtCore/QThread> 
#include <QtCore/QDebug> 
 
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())); // 这里的slot()函数,相当于run()函数的作用
    thread.start(); 
    dummy.emitsig(); 
    return a.exec(); 
}

        结果:恩,slot确实不在主线程中运行(这么简单不值得欢呼么?)

main thread: 0x1a5c 
from thread slot: 0x186c

http://blog.csdn.net/zzwdkxx/article/details/49308487

 

相关文章:

  • 2021-06-06
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2022-01-31
  • 2021-07-16
  • 2022-12-23
猜你喜欢
  • 2022-02-14
  • 2021-07-02
  • 2022-12-23
  • 2021-04-26
  • 2022-12-23
  • 2021-09-23
  • 2021-10-17
相关资源
相似解决方案