logmessage from another thread to the mainwindow slot

  • i have two classes that im trying to log a message to a textbrowser

    1. The main thread works
    2. But if i call the slot and try to emit the singal it does work.
    3. is there a way to debug it
    MainWindow::MainWindow(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::MainWindow)
        , m_window2(new Editor(this))
    , m_window3(new T_Editor(this))
        connect(m_window2, & Editor::logMessage, this, &MainWindow::logMessage);
    // new thread editor 
    connect(m_window3, & T_Editor::logMessage, this, &MainWindow::logMessage);

    If i try to emit logmessage from the other thread it doesnt work. But if i emit from editor class it works. (both in the same thread)

  • Moderators

    In Qt, whether signal-slot connection crosses a thread boundary or not does not matter - it works same regardless of threads. The only thing which might not work is when custom objects are sent - but then all that is needed is to properly register them. Qt warns about it at compile time - so it's not a problem in this case.

    • are you sure the signal logMessage is emitted?
    • if you are using a worker object, are you sure it emits the signal and that you connect to that signal, and not the encompassing thread?
    • are you moving m_window3 to another thread after calling connect()? In that case, it may not work
    • does it work when you hard-code connection type to Qt::QueuedConnection?
    • is your main window performing some CPU-intensive task, like a long loop or something?

  • m_window3 to another thread after calling connect()? In that case, it may not work.

    belive this is what it is.

    mainwindow logMessage is a private member but guess i will have to make it public to access it from the other thread object

    create a connect in the T_editor class that connects the signal logmessage with slot of mainwindow slot.

    Editor/mainwindow both run on the same thread
    Editor create a thread for T_editor that creates its own object etc..

  • Lifetime Qt Champion

    @Sunfluxgames said in logmessage from another thread to the mainwindow slot:

    m_window3(new T_Editor(this))

    I don't think this object (m_window3) lives in another thread at all...

  • Lifetime Qt Champion


    If you want to log from different threads. You should rather have a dedicated object for that with proper protections in place.

Log in to reply