Important: Please read the Qt Code of Conduct -

Calling functions on a QDialog from a different thread

  • Using QT 4.8.4

    I had to move some code out of the GUI thread, because it changed its behaviour and took so long that I wanted to present a progress window and needed the GUI thread to keep on ticking to update that progress window on screen.

    That same code, after a while, potentially tells an existing QDialog to execute (to gather information from the user). When I first moved the code, this crashed - not unexpected, because a QDialog should only execute in the GUI thread, I understand.

    The QDialog that needs to be executed is created in the GUI thread. Good. I then moved the call to exec() that dialog from the second thread, to the GUI thread. In essence, in the second thread, I have a


    going on. All good so far. Dialog happily executes. When the user pushes "OK" on it, it closes, and thread two carries on. It then calls some other functions on that dialog to fetch the values the user entered.

    Here's the question! What are the rules on calling those other functions? They're functions that fetch simple values; strings and so on. Do those other function calls also need to be moved to the GUI thread? The dialog was made in the GUI thread, and was executed in the GUI thread, and then the user pressed OK and the dialog vanished from view as expected; do I need to move all function calls on that dialog to the GUI thread?

  • Lifetime Qt Champion


    If you are using signals and slots to communicate between your worker object and your GUI them you don' have anything special to do. Qt will handle the communication properly for you.

  • And, you know, totally hypothetically, if I'm not using signals and slots to communicate between worker object and GUI...

    To allow me to get the function's returned value, for example. If I turned the QDialog's function into a SLOT, I'd have to pass a pointer or reference to the SLOT for it to place the return value in. It all seems a bit more faff than a simple function call.

  • Lifetime Qt Champion

    Inter-thread communication is not an easy matter to be done properly.

    No you don't, check the worker object paradigm of QThread's documentation

Log in to reply