Wulf
Wulf

Reputation: 712

Qt Signal calling from second thread works -> no effect on connected slot

I have a Qt-Application. One of my class ViewSimulation has a static-Methode loggingHandler which calls (emit) a class-signal logging. The methode loggingHandlerwill be passed as function-pointer to a c-file publisher.c. publisher.c calls his function-pointer (*logger) in a seperate thread.

At the debugging I'm seeing publisher.c calls ViewSimulation::loggingHandler and the signals logging is emited, but the connected slot does not react. But if a call/emit logging in the main thread, the slot of &ViewGooseList::logging) react.

Why the slot is not reacting by "calling" from the other c-thread in publisher.c?

ViewSimulation.cpp/h

class ViewSimulation : public QGroupBox
{
...
    signals:
          void logging(int id, uint64_t timestamp);
    private:
          static void loggingHandler(int id, uint64_t timestamp);
    ViewSimulation* ViewSimulation::m_current;
...
    void ViewSimulation::loggingHandler(int id, uint64_t timestamp)
    {
          emit ViewSimulation::m_current->logging(id, timestamp);
    }
...
    connect(m_gooseSimulation, &ViewSimulation::logging, m_gooseList, &ViewGooseList::logging);
    setLogging(loggingHandler);

publisher.c/h

/*Header*/
void setLogging(void (*logging)(int, uint64_t));
static void (*logger)(int gooseId, uint64_t timestamp);

/*C-File*/
void setLogging(void (*logging)(int, uint64_t))
{
    logger = logging;
}
...
/*This methode will be called from main-thread **AND** from second thread */
logger(gooseMessage->id, gooseMessage->lastTimeStamp);
...

Upvotes: 0

Views: 116

Answers (1)

Nikos C.
Nikos C.

Reputation: 51840

For signal/slot connections across threads, you should create queued connections:

connect(m_gooseSimulation, &ViewSimulation::logging, m_gooseList,
        &ViewGooseList::logging, Qt::QueuedConnection);

Upvotes: 2

Related Questions