然后再定义一个从QThread继承的线程类SubThread,它包含一个SomeObject的对象指针obj,另外有一个slot函数someSolt,定义如下:
- class SubThread : public QThread
- {
- Q_OBJECT
- public:
- SubThread(QObject* parent=0) : QThread(parent){}
- virtual ~SubThread()
- {
- if (obj!=NULL) delete obj;
- }
- public slots:
- // slot function connected to obj's someSignal
- void someSlot();
- public:
- SomeObject * obj;
- };
- // slot function connected to obj's someSignal
- void SubThread::someSlot()
- {
- QString msg;
- msg.append(this->metaObject()->className());
- msg.append("::obj's thread is ");
- if (obj->thread() == qApp->thread())
- {
- msg.append("MAIN thread;");
- }
- else if (obj->thread() == this)
- {
- msg.append("SUB thread;");
- }
- else
- {
- msg.append("OTHER thread;");
- }
- msg.append(" someSlot executed in ");
- if (QThread::currentThread() == qApp->thread())
- {
- msg.append("MAIN thread;");
- }
- else if (QThread::currentThread() == this)
- {
- msg.append("SUB thread;");
- }
- else
- {
- msg.append("OTHER thread;");
- }
- qDebug() << msg;
- quit();
- }
