Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signal/Slot not working
Forum Updated to NodeBB v4.3 + New Features

Signal/Slot not working

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 659 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VincentLiu
    wrote on last edited by VRonin
    #1

    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();
    _commandProcess->moveToThread(_commandProcessThread);
    
    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?

    Thanks.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • hskoglundH Offline
        hskoglundH Offline
        hskoglund
        wrote on last edited by
        #3

        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)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved