Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSignalSpy to receive signals for particular time instead of count



  • is possible to make the QSignalSpy wait for a particular time until it receives the specific signal. Right now I have this function which waits for the particular number of signal count and time. I want this function to wait for waitTimeoutMS and return if it receives a signal with specific data (instead of count). I tried bool QSignalSpy::wait(int timeout = 5000) but it return once it receives first signal.

    bool waitForSpyCalled(QSignalSpy& spy, int waitTimeoutMS, int expectCount = 1) {
      for (int delay = waitTimeoutMS; delay > 0 && spy.count() < expectCount;
           delay -= 200) {
        QTest::qWait(200);
      }
      return spy.count() >= expectCount;
    }
    
    

  • Lifetime Qt Champion

    Hi,

    Do you mean have QSignalSpy start spying only after some delay ?



  • @SGaist Actually I want to look for a particular signal for period of time. Signal will emitted continuously from application.
    Like for example void signalEmitStatus(const EnumType& e);
    Now the enum type will be a list of application state like
    enum EnumType {
    Starting,
    Init,
    Init config,
    Checking peers,
    SUCCESS
    }

    App takes like 1000ms to reach "SUCCESS" state. So each state change gets emitted from signalEmitStatus, but some state will get skipped sometimes that's the reason I can't go with signal emit count. Either I have to collect signals for particular period of time and check the list of signals or need to spy for specific signal.

    I want to catch that SUCCESS state signal


  • Lifetime Qt Champion

    Then I'd rather go with the inspection of the received value. That is more solid than relying on specific timing that might change with machine power.



  • @SGaist you mean to inspect each signal emit until I find SUCCESS. Is it possible to collect signals for particular period of time ?


  • Lifetime Qt Champion

    I think that you are looking for qWaitFor.