Using QThread and moveToThread properly with QTimer and QTcpSocket

前端 未结 3 1715
星月不相逢
星月不相逢 2021-01-02 22:52

From reading this blog, this blog and some others, Subclassing QThread is bad practice. So I tried to apply this method.

But my problem is that I have a QTimer and a

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-02 23:52

    I stumbled across this when searching on Timer behaviour and movetoThread. The accepted answer is a good work-around but not really the root cause of the problem. There is a general rule that when you move an object then all child objects will move as well. So you just need to make sure that the QTimer becomes a child so pass the this pointer in its constructor.

    CommsHandlerIPL::CommsHandlerIP()
    : QObject(), m_pTimer(new QTimer(this))        // <=== crucial to make it a "child" object 
    {
    }
    

提交回复
热议问题