[SOLVED] SIGNAL/SLOT cannot work with Qt:QueuedConnection



  • Hi,

    I was working on Terminal example of Qt, and encountered a problem that, the SLOT never work once Qt::QueuedConnection added.
    Codes:

    struct Settings {
        QString name;
        qint32 baudRate;
        QString stringBaudRate;
        QSerialPort::DataBits dataBits;
        QString stringDataBits;
        QSerialPort::Parity parity;
        QString stringParity;
        QSerialPort::StopBits stopBits;
        QString stringStopBits;
        QSerialPort::FlowControl flowControl;
        QString stringFlowControl;
        bool localEchoEnabled;
    };
    connect(this, SIGNAL(emitOpenSerialOrder(Settings)), myDefaulSerialObject, SLOT(openSerialPort(Settings)));
    

    The codes above work well with out Qt::QueuedConnection; however, once I add it,

    connect(this, SIGNAL(emitOpenSerialOrder(Settings)), myDefaulSerialObject, SLOT(openSerialPort(Settings)), Qt::QueuedConnection);
    

    the SLOT never work.

    This problem stops me from moving myDefaulSerialObject into a new thread!

    Could anyone tell me how to fix this?
    Thank you!



  • Hi. Why should you specify Qt::QueuedConnection? If the object is in another thread, queuing the signals is already the default behavior. Why not let Qt decide? See:

    http://doc.qt.io/qt-5/qt.html#ConnectionType-enum

    Qt::AutoConnection (Default) If the receiver lives in the thread that emits the signal, Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used. The connection type is determined when the signal is emitted.



  • @Leonardo said:

    f the receiver lives in the thread that emits the signal, Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used. The connection type is determined when the signal is emitted.

    Hi,

    Glad to see you here!

    Actually, I am confused. The slot should work even if I specify it to be Qt::QueuedConnection.
    Then why it doesn't work?

    Also, whenever I move the object into a new thread,

    mySerialThread = new QThread();
    myDefaulSerialObject->moveToThread(mySerialThread);
    mySerialThread->start();
    

    it doesn't work either even without my specifying of Qt::QueuedConnection.

    This is really weird because all the other SIGNAL/SLOT have no problem with Qt::QueuedConnection, and the only difference is the Structure argument Settings.



  • Now that you mentioned it... Again, see:

    http://doc.qt.io/qt-5/qt.html#ConnectionType-enum

    With queued connections, the parameters must be of types that are known to Qt's meta-object system, because Qt needs to copy the arguments to store them in an event behind the scenes.
    ...
    Call qRegisterMetaType() to register the data type before you establish the connection.

    Maybe that's what you're missing.



  • @Leonardo said:

    nections, the parameters must be of types that are known to Qt's meta-object system, because Qt needs to copy the arguments to store them in an event behind the scenes.
    ...
    Call qRegisterMetaType() to register the data type before you establish the connectio

    Wow, that really helps!

    Thank you so much!

    I will try it right now



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