您的位置:首页 > 编程语言 > Qt开发

Qt中使用线程时的注意事项(线程没起作用的原因)

2017-01-17 17:29 357 查看
今天偶然发现,运行程序时程序输出窗口中有如下提示:

 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对象
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Qt5 QThread 无效 线程