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

来源:互联网 发布:lol 4k分辨率优化 编辑:程序博客网 时间:2024/05/15 10:24

今天偶然发现,运行程序时程序输出窗口中有如下提示:

 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对象

0 0
原创粉丝点击