Update GUI from QThreadPool



  • Hi Everyone,

    I am running a server with QThreadPool to manage worker threads. I have a GUI I'd like to update from inside a thread managed by the QThreadPool. I am having trouble figuring out how to connect the proper slots and signals so that my Runnable object can update my GUI on the main thread. How is this done?

    Thank you!


  • Lifetime Qt Champion

    Hi,

    You can't modify a GUI element from a worker thread. You can use e.g. a worker object that you will connect to your GUI before moving it to a different thread.

    Hope it helps



  • First create a Slot in your GUI Object.
    Lets say you have an QWidget. Than you have to create a private slot like :

    private slots:
        void updateMyGui();    
    

    Then create a signal in your Runnable object.

    signals:
        void notifyGUI();
    

    And now when your create your Runnable object pass your widget as a pointer and simply connect your runnable signal tour your gui object slot :

    connect(this, SIGNAL(notifyGUI()), guiObjectPointer, SLOT(updateMyGui()));
    

    When you want to update your gui in your runnable you have to emit notifyGUI signal :

    emit notifyGUI();
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.