Signal emitted but slot not getting called?



  • I have a signal:

    signal:
        void write(QJsonObject& robjJSON);
    

    In the same class I have a slot:

    public slots:
        void onWrite(QJsonObject& robjJSON);
    

    In my constructor I connect the signal and slot:

    QObject::connect(this, &clsMsgSender::write
                    ,this, &clsMsgSender::onWrite);
    

    The signal is being emitted and I've verified this with the debugger, but the slot which has a breakpoint on the first line of the function does not get called:

    void clsMsgSender::onWrite(QJsonObject& robjJSON) {
        QMutexLocker lock(&mMutex);
        //Add the message ID    
        robjJSON.insert(clsMsgSender::mscszMsgID, QString::number(++muint32MID));
        //Associate this TCP socket with the output data stream
        QByteArray arybytMsg;
        arybytMsg = QJsonDocument(robjJSON).toJson(QJsonDocument::Compact);
        //Write message
        qint64 int64Written = mpsckClient->write(arybytMsg);
    
        if ( int64Written > 0 ) {
            QString strMsg(arybytMsg);
            qdbg() << QString("onWrite::run[%1]: %2").arg(int64Written).arg(strMsg);
        }
    }
    

    Is there anything I can do that would help me find out why it isn't working?



  • Found the issue:

    QObject::connect: Cannot queue arguments of type 'QJsonObject&'
    (Make sure 'QJsonObject&' is registered using qRegisterMetaType().)
    

    However, I was unable to find a way to reference a reference to an object as a parameter, so in the end I had to change the parameter type to a pointer.

    QJsonObject*
    


  • Found the issue:

    QObject::connect: Cannot queue arguments of type 'QJsonObject&'
    (Make sure 'QJsonObject&' is registered using qRegisterMetaType().)
    

    However, I was unable to find a way to reference a reference to an object as a parameter, so in the end I had to change the parameter type to a pointer.

    QJsonObject*
    

  • Moderators

    @SPlatten said in Signal emitted but slot not getting called?:

    However, I was unable to find a way to reference a reference to an object as a parameter, so in the end I had to change the parameter type to a pointer.

    Because between threads a reference to an object makes very little sense with a queued connection. What will have happened is you getting a dangling reference after scope of the emission goes out and your receiver operating over an invalid object. Queuing a pointer makes just as little sense in this case, just use a plain value, QJsonObject is already a value-class and does the right thing.

    To use references directly one'd switch to Qt::DirectConnection but then one must make sure access it thread-safe (manually ensuring that).

    PS.
    In your example that'd mean switching to const QJsonObject &, and Qt is going to serialize the object(s) properly.


Log in to reply