1.面向对象程序设计实践的早期工程中习惯于通过继承的方式扩展系统的功能2.解决方案——信号与槽在类中定义一个槽函数void tmain()作为线程入口函数在类中定义一个QThread成员对象m_thread改变当前对象的线程依附性到m_thread连接m_thread的start()信号到tmain()AnotherThread.h#ifndef ANOTHERTHREAD_H #define ANOTHERTHREAD_H #include QObject #include QThread class AnotherThread : public QObject { Q_OBJECT QThread m_thread; protected slots: void tmain(); public: explicit AnotherThread(QObject *parent nullptr); void start(); void ternimate(); void exit(int c); ~AnotherThread(); signals: }; #endif // ANOTHERTHREAD_HAnotherThread.cpp#include AnotherThread.h #include QDebug AnotherThread::AnotherThread(QObject *parent) : QObject{parent} { moveToThread(m_thread); connect(m_thread, SIGNAL(started()), this, SLOT(tmain())); } void AnotherThread::tmain() { qDebug() void AnotherThread::tmain() tid QThread::currentThreadId(); for(int i0; i10; i) { qDebug() void AnotherThread::tmain() i i; } qDebug() void AnotherThread::tmain() end; } void AnotherThread::start() { m_thread.start(); } void AnotherThread::ternimate() { m_thread.terminate(); } void AnotherThread::exit(int c) { m_thread.exit(c); } AnotherThread::~AnotherThread() { m_thread.wait(); }main.cpp#include QCoreApplication #include QDebug #include QThread #include AnotherThread.h void test() { AnotherThread at; at.start(); } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug() main() tid QThread::currentThreadId(); test(); return QCoreApplication::exec(); }运行结果main() tid 0x2ee4void AnotherThread::tmain() tid 0x38a8void AnotherThread::tmain() i 0void AnotherThread::tmain() i 1void AnotherThread::tmain() i 2void AnotherThread::tmain() i 3void AnotherThread::tmain() i 4void AnotherThread::tmain() i 5void AnotherThread::tmain() i 6void AnotherThread::tmain() i 7void AnotherThread::tmain() i 8void AnotherThread::tmain() i 9void AnotherThread::tmain() end