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
Qt 6.11 is out! See what's new in the release blog

signal/slot not working

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 9 Posters 4.7k Views 5 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.
  • sierdzioS sierdzio

    Not the answer you're expecting, but: I don't see anything painfully obvious in here.

    Both connect() statements are called after the worker is moved to thread, right?

    mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #6

    @sierdzio yes, the signals are called after the worker is moved.

    (how did you know worker was in another thread?)

    sierdzioS 1 Reply Last reply
    0
    • mzimmersM mzimmers

      @sierdzio yes, the signals are called after the worker is moved.

      (how did you know worker was in another thread?)

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #7

      @mzimmers said in signal/slot not working:

      @sierdzio yes, the signals are called after the worker is moved.

      (how did you know worker was in another thread?)

      It's common to call objects working in other threads "workers" :-)

      (Z(:^

      mzimmersM 1 Reply Last reply
      1
      • sierdzioS sierdzio

        @mzimmers said in signal/slot not working:

        @sierdzio yes, the signals are called after the worker is moved.

        (how did you know worker was in another thread?)

        It's common to call objects working in other threads "workers" :-)

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #8

        @sierdzio ah. So...any other ideas on how to chase this down? I've exhausted my own ideas...

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

          Just a guess, but maybe it's a timing problem, if the MSG_WIFI_SETUP_ACK message arrives in the Worker before
          QObject::connect(m_worker, &Worker::wifiSetupAckReceived, this, &WifiSetup::onAckReceived); has been run...

          mzimmersM 1 Reply Last reply
          1
          • hskoglundH hskoglund

            Just a guess, but maybe it's a timing problem, if the MSG_WIFI_SETUP_ACK message arrives in the Worker before
            QObject::connect(m_worker, &Worker::wifiSetupAckReceived, this, &WifiSetup::onAckReceived); has been run...

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #10

            @hskoglund thanks for the suggestion, but that's definitely not the case. The connect statement is run in the c'tor of the QWidget, and the message only comes after the user presses a button, a message is sent out to the remote device and a response is read.

            1 Reply Last reply
            0
            • mzimmersM mzimmers

              Hi all -

              I feel like a Qt rookie again; I remember asking questions like this 10 years ago.

              I'll let the code do the talking:

              class WifiSetup : public QDialog
              {
                  Q_OBJECT
              	...
                  void onMsgReceived(Message *msg);
                  void onAckReceived(Message *msg);
              }
              WifiSetup::WifiSetup()
              {
              	...
                  QObject::connect(m_worker, &Worker::newSerialData, this, &WifiSetup::onMsgReceived);
                  QObject::connect(m_worker, &Worker::wifiSetupAckReceived, this, &WifiSetup::onAckReceived);
              }
              void WifiSetup::onMsgReceived(Message *msg) {...}
              void WifiSetup::onAckReceived(Message *msg) {...}
              
              void Worker::processSerial(QByteArray buff)
              {
                  mt = m_msg.getType();
                  switch (mt)
                  {
                  case MSG_DISCOVERY_ACK:
                      emit newSerialData(&m_msg);
                      break;
                  case MSG_WIFI_SETUP_ACK:
                      emit wifiSetupAckReceived(&m_msg);
                      break;
              	...
              }
              

              When mt == MSG_DISCOVERY_ACK, the signal and slot work correctly.
              When mt == MSG_WIFI_SETUP_ACK, the signal is called, but the slot is never reached.

              I've run clean, qmake and rebuild...same behavior. I'm sure I'm overlooking something painfully obvious, but I sure don't see what.

              Thanks...

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #11

              @mzimmers said in signal/slot not working:

              When mt == MSG_WIFI_SETUP_ACK, the signal is called, but the slot is never reached.

              How do you know it is called? Is the code containing the emit yours/debug-breakpointable by you?

              Is m_worker another thread?

              Add a brand new pair of slots, which do nothing, and attach them too to the signals. Preferably first. Do they behave the same?

              mzimmersM 1 Reply Last reply
              1
              • JonBJ JonB

                @mzimmers said in signal/slot not working:

                When mt == MSG_WIFI_SETUP_ACK, the signal is called, but the slot is never reached.

                How do you know it is called? Is the code containing the emit yours/debug-breakpointable by you?

                Is m_worker another thread?

                Add a brand new pair of slots, which do nothing, and attach them too to the signals. Preferably first. Do they behave the same?

                mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by mzimmers
                #12

                @JonB said in signal/slot not working:

                How do you know it is called? Is the code containing the emit yours/debug-breakpointable by you?
                Is m_worker another thread?

                Yes, and yes. The emit is in my first post.

                Add a brand new pair of slots, which do nothing, and attach them too to the signals. Preferably first. Do they behave the same?

                    QObject::connect(m_worker, &Worker::wifiSetupAckReceived, this, &WifiSetup::testSlot1);
                    QObject::connect(m_worker, &Worker::wifiSetupAckReceived, this, &WifiSetup::testSlot2);
                    QObject::connect(m_worker, &Worker::wifiSetupAckReceived, this, &WifiSetup::onAckReceived);
                void WifiSetup::testSlot1()
                {
                    qDebug() << "testSlot1 reached.";
                }
                void WifiSetup::testSlot2()
                {
                    qDebug() << "testSlot2 reached.";
                }
                

                I put breakpoints on all three slots...none are hit. And it's not some debugger error; no action by the slot(s) is taken.

                JonBJ 1 Reply Last reply
                0
                • mzimmersM mzimmers

                  @JonB said in signal/slot not working:

                  How do you know it is called? Is the code containing the emit yours/debug-breakpointable by you?
                  Is m_worker another thread?

                  Yes, and yes. The emit is in my first post.

                  Add a brand new pair of slots, which do nothing, and attach them too to the signals. Preferably first. Do they behave the same?

                      QObject::connect(m_worker, &Worker::wifiSetupAckReceived, this, &WifiSetup::testSlot1);
                      QObject::connect(m_worker, &Worker::wifiSetupAckReceived, this, &WifiSetup::testSlot2);
                      QObject::connect(m_worker, &Worker::wifiSetupAckReceived, this, &WifiSetup::onAckReceived);
                  void WifiSetup::testSlot1()
                  {
                      qDebug() << "testSlot1 reached.";
                  }
                  void WifiSetup::testSlot2()
                  {
                      qDebug() << "testSlot2 reached.";
                  }
                  

                  I put breakpoints on all three slots...none are hit. And it's not some debugger error; no action by the slot(s) is taken.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #13

                  @mzimmers
                  Earlier you said:

                  When mt == MSG_DISCOVERY_ACK, the signal and slot work correctly.

                  When mt == MSG_WIFI_SETUP_ACK, the signal is called, but the slot is never reached.

                  Good grief! I meant, a new pair, one on newSerialData, one on wifiSetupAckReceived. Not 3 on one and one on the other! Wanted to check the two signals each behaved the same as you reported (one good, one bad) on new slots.

                  Dunno, bet it's a thread thing :)

                  mzimmersM 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @mzimmers
                    Earlier you said:

                    When mt == MSG_DISCOVERY_ACK, the signal and slot work correctly.

                    When mt == MSG_WIFI_SETUP_ACK, the signal is called, but the slot is never reached.

                    Good grief! I meant, a new pair, one on newSerialData, one on wifiSetupAckReceived. Not 3 on one and one on the other! Wanted to check the two signals each behaved the same as you reported (one good, one bad) on new slots.

                    Dunno, bet it's a thread thing :)

                    mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by
                    #14

                    @JonB said in signal/slot not working:

                    Good grief! I meant, a new pair, one on newSerialData, one on wifiSetupAckReceived. Not 3 on one and one on the other! Wanted to check the two signals each behaved the same as you reported (one good, one bad) on new slots.

                    Sigh...my brain's still in low gear.

                        QObject::connect(m_worker, &Worker::newSerialData, this, &WifiSetup::testSlot1);
                        QObject::connect(m_worker, &Worker::newSerialData, this, &WifiSetup::onMsgReceived);
                        QObject::connect(m_worker, &Worker::wifiSetupAckReceived, this, &WifiSetup::testSlot2);
                        QObject::connect(m_worker, &Worker::wifiSetupAckReceived, this, &WifiSetup::onAckReceived);
                    

                    Both slots for newSerialData get hit; neither of the ones for wifiSetupAckReceived get hit.

                    Dunno, bet it's a thread thing :)

                    What's confounding about this is that one works, and the other doesn't. Seemingly identical.

                    JonBJ 1 Reply Last reply
                    0
                    • mzimmersM mzimmers

                      @JonB said in signal/slot not working:

                      Good grief! I meant, a new pair, one on newSerialData, one on wifiSetupAckReceived. Not 3 on one and one on the other! Wanted to check the two signals each behaved the same as you reported (one good, one bad) on new slots.

                      Sigh...my brain's still in low gear.

                          QObject::connect(m_worker, &Worker::newSerialData, this, &WifiSetup::testSlot1);
                          QObject::connect(m_worker, &Worker::newSerialData, this, &WifiSetup::onMsgReceived);
                          QObject::connect(m_worker, &Worker::wifiSetupAckReceived, this, &WifiSetup::testSlot2);
                          QObject::connect(m_worker, &Worker::wifiSetupAckReceived, this, &WifiSetup::onAckReceived);
                      

                      Both slots for newSerialData get hit; neither of the ones for wifiSetupAckReceived get hit.

                      Dunno, bet it's a thread thing :)

                      What's confounding about this is that one works, and the other doesn't. Seemingly identical.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #15

                      @mzimmers
                      Send me $5 if it turns out you're doing something else in your code causing this behaviour....

                      mzimmersM 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @mzimmers
                        Send me $5 if it turns out you're doing something else in your code causing this behaviour....

                        mzimmersM Offline
                        mzimmersM Offline
                        mzimmers
                        wrote on last edited by mzimmers
                        #16

                        @JonB define "something else."

                        Hell, I should send you a bottle for all the help you've given me over the years...

                        Of course, by an extension of that reasoning, I'd have to buy SGaist his own distillery.

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

                          Hi,

                          @mzimmers said in signal/slot not working:

                          mt = m_msg.getType();
                          switch (mt)

                          When is m_msg updated ? You could be processing the same value twice.

                          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
                          • mzimmersM Offline
                            mzimmersM Offline
                            mzimmers
                            wrote on last edited by
                            #18
                            void Worker::processSerial(QByteArray buff)
                            {
                                MsgType mt;
                                string s;
                            
                                s.assign(buff);
                            
                                m_msg.decodeXml(s);
                                mt = m_msg.getType();
                                switch (mt)
                                {
                                case MSG_DISCOVERY_ACK:
                                    emit newSerialData(&m_msg);
                                    break;
                                case MSG_WIFI_SETUP_ACK:
                                    emit wifiSetupAckReceived(&m_msg);
                                    break;
                            ...
                            

                            Besides, the signal is being emitted (at least according to the debugger).

                            1 Reply Last reply
                            0
                            • sierdzioS Offline
                              sierdzioS Offline
                              sierdzio
                              Moderators
                              wrote on last edited by
                              #19

                              Insert a qDebug() before the emit to be 100% sure.

                              One more idea... this method isn't called once, is it? Because if it is called once, and your have a break; in your switch then only the first ack will trigger MSG_DISCOVERY_ACK, then it will break and never bother to check if MSG_WIFI_SETUP_ACK is satisfied or not.

                              (Z(:^

                              1 Reply Last reply
                              0
                              • JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #20

                                Let's be 100% clear for everyone: because of your separate threads, these are queued-connection signals, right? So the slots won't be called during the emits, only later on, and we're all clear about this, right?

                                @sierdzio said in signal/slot not working:

                                Insert a qDebug() before the emit to be 100% sure.

                                Damn right, I'd have expected you to do this to make sure already!

                                sierdzioS 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  Let's be 100% clear for everyone: because of your separate threads, these are queued-connection signals, right? So the slots won't be called during the emits, only later on, and we're all clear about this, right?

                                  @sierdzio said in signal/slot not working:

                                  Insert a qDebug() before the emit to be 100% sure.

                                  Damn right, I'd have expected you to do this to make sure already!

                                  sierdzioS Offline
                                  sierdzioS Offline
                                  sierdzio
                                  Moderators
                                  wrote on last edited by
                                  #21

                                  @JonB said in signal/slot not working:

                                  Let's be 100% clear for everyone: because of your separate threads, these are queued-connection signals, right? So the slots won't be called during the emits, only later on, and we're all clear about this, right?

                                  Yes, they will be queued. Good point, that adds one, although rare, possibility: the event loop may be too busy to process the queue.

                                  (Z(:^

                                  1 Reply Last reply
                                  0
                                  • mzimmersM Offline
                                    mzimmersM Offline
                                    mzimmers
                                    wrote on last edited by
                                    #22

                                    Sigh.

                                    I really should never have become a programmer.

                                    This was pure cockpit error on my part. Everyone feel free to negrate me.

                                    And Jon, email me your postal address; I'll send you a bottle of the good stuff.

                                    Sorry, everyone.

                                    J.HilkJ 1 Reply Last reply
                                    0
                                    • mzimmersM mzimmers

                                      Sigh.

                                      I really should never have become a programmer.

                                      This was pure cockpit error on my part. Everyone feel free to negrate me.

                                      And Jon, email me your postal address; I'll send you a bottle of the good stuff.

                                      Sorry, everyone.

                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #23

                                      @mzimmers So, what was it, not be shy, we won't judge :D

                                      I was about to suggest changing the argument of your signals to actual objects (not pointers or references) to force the compiler the acknowledge that copying has to be done, and that the lifetime of the objects do not interfere.


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      mzimmersM 1 Reply Last reply
                                      0
                                      • sierdzioS Offline
                                        sierdzioS Offline
                                        sierdzio
                                        Moderators
                                        wrote on last edited by
                                        #24

                                        Do not despair :-) I'm pretty sure all of us here had spent a whole day (or 3...) looking for some nasty bug only to discover a missing semicolon :D

                                        (Z(:^

                                        J.HilkJ 1 Reply Last reply
                                        1
                                        • sierdzioS sierdzio

                                          Do not despair :-) I'm pretty sure all of us here had spent a whole day (or 3...) looking for some nasty bug only to discover a missing semicolon :D

                                          J.HilkJ Offline
                                          J.HilkJ Offline
                                          J.Hilk
                                          Moderators
                                          wrote on last edited by J.Hilk
                                          #25

                                          @sierdzio said in signal/slot not working:

                                          Do not despair :-) I'm pretty sure all of us here had spent a whole day (or 3...) looking for some nasty bug only to discover a missing semicolon :D

                                          very true, my personal favorite:

                                          if(conditionA == true);
                                               callFunctionA();
                                          

                                          and callFunctionA() always being executed. Now days QtC warns you about it, but that wasn't always the case. Long days of debugging ....


                                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                          Q: What's that?
                                          A: It's blue light.
                                          Q: What does it do?
                                          A: It turns blue.

                                          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