今天偶然发现,运行程序时程序输出窗口中有如下提示:
QObject::startTimer: Timers can only be used with threads started with QThread
也就是当对象有父对象时,是不可以移到其他线程当中去的。
代码如下:m_Flower为自定义对象,flowerThead为线程。
**不起作用的代码:
m_Flower=new DispatchFlower(this);
flowerThread=new QThread();
m_Flower->moveToThread(flowerThread);
connect(this,SIGNAL(sendQuery_GetRTSheetList(QString)),m_Flower,SLOT(getRTDispatchSheetList(QString)));
connect(m_Flower,SIGNAL(haveGotDispatchSheetList(QList<DispatchSheet>&)),this,SLOT(editMultiSheets(QList<DispatchSheet>&)));
flowerThread->start();
**修改后起作用的代码:
m_Flower=new DispatchFlower();
flowerThread=new QThread();
m_Flower->moveToThread(flowerThread);
connect(this,SIGNAL(sendQuery_GetRTSheetList(QString)),m_Flower,SLOT(getRTDispatchSheetList(QString)));
connect(m_Flower,SIGNAL(haveGotDispatchSheetList(QList<DispatchSheet>&)),this,SLOT(editMultiSheets(QList<DispatchSheet>&)));
flowerThread->start();
注意:因m_Flower没有指定父对象,需要在析构函数中删除此m_Flowe对象