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 emitted but slot not getting called?
Forum Updated to NodeBB v4.3 + New Features

Signal emitted but slot not getting called?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 677 Views 2 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    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?

    Kind Regards,
    Sy

    SPlattenS 1 Reply Last reply
    0
    • SPlattenS SPlatten

      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?

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by SPlatten
      #2

      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*
      

      Kind Regards,
      Sy

      kshegunovK 1 Reply Last reply
      0
      • SPlattenS SPlatten

        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*
        
        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @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.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        6

        • Login

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