Qt threading stopped UI from responding

前端 未结 1 744
盖世英雄少女心
盖世英雄少女心 2021-01-26 08:20

I think I might misunderstood several concepts in Qt\'s threading,

In my window class, which is derived from QWidget:

class Widget 
{
   Q_OBJECT
public:         


        
相关标签:
1条回答
  • 2021-01-26 08:57

    Widget::dowork() is executed on the main thread (on which the GUI runs), that's why it blocks. It doesn't matter that it was called by a QThread.

    The correct way to execute code on another thread is to first move a QObject instance to a QThread using QObject::moveToThread(), and then connect the started() signal of the QThread to the slot of the QObject instance that you want executed.

    If you want to know more: http://blog.qt.digia.com/2010/06/17/youre-doing-it-wrong/

    Another issue with your code is that you're trying to move a QWidget-derived object to another thread. This is not allowed. QWidget instances must remain on the main thread. Instead, you should subclass from QObject.

    Yet another issue with the code is that you're doing this in the constructor. Moving the object to another thread while it's not fully constructed yet is just asking for trouble.

    0 讨论(0)
提交回复
热议问题