class CMainWindow : public QMainWindow{ Q_OBJECT.......private: QThread mThread;.......};CMainWindow::CMainWindow(QWidget *parent) : QMainWindow(parent), mpUi(new Ui::CMainWindow){....... mThread.start(); CSomeClass *s = new CSomeClass; connect(&mThread, SIGNAL(finished()), s, SLOT(deleteLater())); s->moveToThread(&mThread);.......}CMainWindow::~CMainWindow(){ mThread.quit(); mThread.wait();}
class CMainWindow : public QMainWindow{ Q_OBJECT.......private: QThread mThread; CSomeClass *mpS;.......};CMainWindow::CMainWindow(QWidget *parent) : QMainWindow(parent), mpUi(new Ui::CMainWindow){....... connect(&mThread, SIGNAL(finished()), this, SLOT(deleteListener()));.......}void CMainWindow::closeEvent(QCloseEvent * e){ on_actConnectToServer_toggled(false); e->accept();}void CMainWindow::on_actConnectToServer_toggled( bool v ){ if(v) { mpS = new CSomeClass; mpS->moveToThread(&mThread); mThread.start(); } else { mThread.quit(); mThread.wait(); }}void CMainWindow::deleteListener(){ delete mpS;}