注意:释放线程的时候触发线程的信号与槽连接时的连接类型参数一定要是Qt::ConnectionType::DirectConnection,否则线程销毁不了会造成内存泄露,通过任务栏开启资源管理器可监视cup的线程数变化情况。
QThread* th=new QThread();
Work* mywork=new Work ();
mywork->moveToThread(th);
SigManager sm;
connect(th, &QThread::finished, mywork, &QObject::deleteLater);//线程结束释放工作类
connect(th, &QThread::finished, th, &QObject::deleteLater);//线程结束释放线程指针
QObject::connect(mywork,&Work::SigExitThread,th,&QThread::exit,Qt::ConnectionType::DirectConnection);//退出线程
QObject::connect(mywork,&Work::SigExitThread,th,&QThread::quit,Qt::ConnectionType::DirectConnection);//退出线程
QObject::connect(mywork,&Work::SigExitThread,th,&QThread::terminate,Qt::ConnectionType::DirectConnection);//退出线程
QObject::connect(&sm, &SigManager::Sig_ExcuteFunction,mywork,&Work::Function1);
th->start();
版权归原作者 luckyone906 所有, 如有侵权,请联系我们删除。