http://mobile.51cto.com/symbian-268690.htm
定义一个 Dummy 类,在run中发射它的信号
也可以在run中发射 Thread 中的信号,而不是Dummy(效果完全一样),QThread 定义槽函数,重载run函数
- /*!
- * \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: oid emitsig()
- { emit sig();
- } signals: void sig(); };
- class Thread:public QThread {
- Q_OBJECT public:
- Thread(QObject* parent=0):QThread(parent) { //moveToThread(this); }
- public slots: void slot_thread() {
- qDebug()<<"from thread slot_thread:" <<currentThreadId(); }
- signals: void sig(); protected: void run() {
- qDebug()<<"thread thread:"<<currentThreadId();
- Dummy dummy;
- connect(&dummy, SIGNAL(sig()), this, SLOT(slot_thread()));
- dummy.emitsig(); e
- xec();
- }
- };
- #include "main.moc" int main(int argc, char *argv[]) {
- QCoreApplication a(argc, argv);
- qDebug()<<"main thread:"<<QThread::currentThreadId();
- Thread thread;
- thread.start();
- return a.exec(); }