wrong way, 即在Qt.建立read>。 http://doc.qt.nokia.com/stable/qobject.html#move ToThread”rel=“nofollow”move ToThread
这只是允许你改变物体及其子女的亲子关系。
Changes the thread affinity for this object and its children. The
object cannot be moved if it has a parent. Event processing will
continue in the targetThread.
To move an object to the main thread, use QApplication::instance() to
retrieve a pointer to the current application, and then use
QApplication::thread() to retrieve the thread in which the application
lives.
因此,你应当做的是继承<代码>QObject而不是QThread
,并改变您的运行功能,将B物体移至其他深层。
抽样法(未经过测试)
class B : public QObject {
Q_OBJECT
public:
void run() {
}
};
class A : public QObject {
Q_OBJECT
public:
void run() {
b1Thread = new QThread;
b2Thread = new QThread;
b1.moveToThread(b1Thread);
b2.moveToThread(b2Thread);
b1.run();
b2.run();
}
protected:
B b1, b2;
private:
QThread* b1Thread, b2Thread; // Delete them in the destructor.
};
您可在<代码>main.cpp上构筑read,并将其通过<代码>。 B 为论据。
关于<代码>move ToThread的通知
this function can only "push" an object from the current thread to
another thread, it cannot "pull" an object from any arbitrary thread
to the current thread.