Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Threads and signals



  • Hi all!
    Sorry if my question is already solved but I can't found solution for me yet.

    I need to understand using of signals and slots across threads that is not in relation.

    For example, I have:

    1. Main GUI application named "main_ui";
    2. Main thread named "thread_main" that controls...
    3. ... set of threads (named "thread_#" respectively).

    "main_ui" contents a widget for logging of all application events.
    "main_ui" creates "thread_main" and they connected using slots well. "thread_main" is allowed to use "main_ui"'s log widget.
    "thread_#" created by "thread_main".
    How to connect "thread_#" and "main_ui" for use of "main_ui"'s log widget by "thread_#" directly?

    Creation order: main_ui => thread_main => thread_#


  • Lifetime Qt Champion

    @alxdef said in Threads and signals:

    and how to connect main_ui slot with thread_#?

    Since you can connect signals to signals you can simply connect the signals from thread_# to corresponding signals in thread_main and connect slots in main_ui to signals from thread_main.


  • Lifetime Qt Champion

    @alxdef said in Threads and signals:

    "thread_main" is allowed to use "main_ui"'s log widget.

    This is not going to work: only GUI thread is allowed to access UI!



  • @jsulm, I think I say incorrectly. I mean thread_# can send signal to main_ui with parameters for log widget.



  • @alxdef
    There are several ways you could approach this, depending on what is "natural" to your code.

    First, your:

    "thread_main" is allowed to use "main_ui"'s log widget.

    Be careful about what you mean/implement for this. A thread should never access any widget in the UI thread. Depends what exactly you do here.

    That apart: when thread_main creates a thread_#, make it emit a signal. main_ui should have a slot on that signal, and receive whatever parameter necessary for it to do the required connect() on whatever signal it will receive in future from thread_# which notifies it to change its widget.

    Note that in all cases we have said threads must send signals to which main_ui is connected, and only main_ui accesses the widget.



  • @JonB, and how to connect main_ui slot with thread_#?



  • @alxdef said in Threads and signals:

    @JonB, and how to connect main_ui slot with thread_#?

    Exactly as I wrote:

    That apart: when thread_main creates a thread_#, make it emit a signal. main_ui should have a slot on that signal, and receive whatever parameter necessary for it to do the required connect() on whatever signal it will receive in future from thread_# which notifies it to change its widget.

    EDIT
    Or you can indeed choose to "chain" signals as @jsulm writes below, using thread_main as an "intermediary". Both approaches are valid.


  • Lifetime Qt Champion

    @alxdef said in Threads and signals:

    and how to connect main_ui slot with thread_#?

    Since you can connect signals to signals you can simply connect the signals from thread_# to corresponding signals in thread_main and connect slots in main_ui to signals from thread_main.


Log in to reply