Signal/Slot not working

  • The flow of my code: Code flow <---- picture link

    Hi, I have an signal/slot pair that doesn't work as expected. Can anyone help me ?

    There are three classes: InspectionHandler, CommandProcess and SerialPortCtrl

    The class InspectionHandler and SerialPortCtrl are declared in main.cpp as followed:

    SerialPortCtrl *serialPortCtrl = new SerialPortCtrl;
    InspectionHandler *inspectionHandler = new InspectionHandler(0);
    QObject::connect(serialPortCtrl, &SerialPortCtrl::sendResponseSignal, inspectionHandler, &InspectionHandler::receiveResponseSlot);
    QObject::connect(inspectionHandler, &InspectionHandler::sendRequestSignal, serialPortCtrl, &SerialPortCtrl::receiveRequestSlot);

    And I declare the CommandProcess instance in the InspectionHandler constructor:

    _commandProcessThread = new QThread();
    _commandProcess = new CommandProcess();
    QObject::connect(_commandProcess, SIGNAL(initCompletedSignal()), _commandProcessThread, SLOT(start()));
    QObject::connect(_commandProcessThread, SIGNAL(started()), _commandProcess, SLOT(processSlot()));
    QObject::connect(_commandProcess, &CommandProcess::sendRequestSignal, this, &InspectionHandler::receiveRequest);
    // ========= isConnected : true, but the connection does not really work ====================
    bool isConnected = static_cast<bool>(QObject::connect(this, &InspectionHandler::sendResponse, _commandProcess, &CommandProcess::receiveResponseSlot), Qt::DirectConnection);
        // QObject::connect(_commandProcess, &CommandProcess::finishProcessSignal, _commandProcessThread, &QThread::quit);
        // QObject::connect(_commandProcessThread, &QThread::finished, _commandProcess, &CommandProcess::deleteLater);
        // QObject::connect(_commandProcessThread, &QThread::finished, _commandProcessThread, &QThread::deleteLater);

    Several questions need your suggestion:

    1. The isConnected is true. However, the CommandProcess::receiveResponseSlot doesn't receive the signal from &InspectionHandler::sendResponse, do I make any mistake? The error could be resulted from multiThreading design ?
    2. Compare to the CommandProcess instance declared in InspectionHandler, I hope the control of serialport could be unique (as it is physically unique), so declare it as SerialPortCtrl *serialPortCtrl = new SerialPortCtrl in the main.cpp. Is it a good idea?


  • Lifetime Qt Champion


    Can you show the signal and slot signatures ?

    By the way, why are you static casting your connect call ? QObject::connect already returns a bool.

  • Hi, also about that Qt::DirectConnection, make sure it's not included in your connect() call, because it doesn't work across threads (you need to use Qt::AutoConnection or Qt::QueuedConnection)

Log in to reply

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