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. Timer with Lambda function
Forum Updated to NodeBB v4.3 + New Features

Timer with Lambda function

Scheduled Pinned Locked Moved Unsolved General and Desktop
35 Posts 6 Posters 5.8k 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.
  • S SPlatten
    16 Dec 2020, 08:06

    @jsulm , what else would you suggest? I don't want to static allocate and I cannot use the stack.

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 16 Dec 2020, 08:09 last edited by
    #6

    @SPlatten said in Timer with Lambda function:

    what else would you suggest?

    Make it member instead of a pointer? What's the point to have it as pointer allocate memory on the heap (which is slower) and then care to not to forget to delete it?

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    1
    • S SPlatten
      16 Dec 2020, 07:49

      I am sending JSON messages between processes and I want start a timer when the message is sent, if no acknowledge is received then the timer will timeout and the same message will be written again, at least that's the intention.

      I have a structure:

      typedef struct {
          QJsonObject objMsg;
          QTimer* pTimer;
      } tAckMsgTracking;
      
      typedef std::map<qulonglong, tAckMsgTracking*> mmpAck;
      

      The map is keyed by a message transaction ID which is uniquely generated when the message is sent. The problem I'm having is that the lambda slot does not get called:

      tAckMsgTracking* pAckMsgTrkr = new tAckMsgTracking;
      pAckMsgTrkr->objMsg = objJSON;
      pAckMsgTrkr->pTimer = new QTimer(this);
      QObject::connect(pAckMsgTrkr->pTimer, &QTimer::timeout, [this, pAckMsgTrkr]() {
      //Re-send message
          emit write(pAckMsgTrkr->objMsg);
      });
      //Insert a unique message ID into the message
      objJSON.insert(clsJSON::mscszMsgID, QString::number(++clsMsgSender::msulnglngMsgID));
      //Insert entry into the map
      clsMsgSender::msmpAcks.insert(std::make_pair(clsMsgSender::msulnglngMsgID, pAckMsgTrkr));
      //Start the timer that will resend message if ack. not received
      pAckMsgTrkr->pTimer->start(clsMsgSender::mscuint16AckTimeout);
      

      The value of 'mscuint16AckTimeout' is 5000 (milliseconds). I've also tried:

      pAckMsgTrkr->pTimer->start(std::chrono::milliseconds(clsMsgSender::mscuint16AckTimeout));
      

      I have a break point in the lambda slot, it doesn't get hit.

      J Offline
      J Offline
      JonB
      wrote on 16 Dec 2020, 08:11 last edited by JonB
      #7

      @SPlatten
      Since the code looks OK (other than heap issue), try a qDebug() instead of relying on breakpoint, and move the timeout down to 0 to debug.

      1 Reply Last reply
      0
      • S SPlatten
        16 Dec 2020, 07:49

        I am sending JSON messages between processes and I want start a timer when the message is sent, if no acknowledge is received then the timer will timeout and the same message will be written again, at least that's the intention.

        I have a structure:

        typedef struct {
            QJsonObject objMsg;
            QTimer* pTimer;
        } tAckMsgTracking;
        
        typedef std::map<qulonglong, tAckMsgTracking*> mmpAck;
        

        The map is keyed by a message transaction ID which is uniquely generated when the message is sent. The problem I'm having is that the lambda slot does not get called:

        tAckMsgTracking* pAckMsgTrkr = new tAckMsgTracking;
        pAckMsgTrkr->objMsg = objJSON;
        pAckMsgTrkr->pTimer = new QTimer(this);
        QObject::connect(pAckMsgTrkr->pTimer, &QTimer::timeout, [this, pAckMsgTrkr]() {
        //Re-send message
            emit write(pAckMsgTrkr->objMsg);
        });
        //Insert a unique message ID into the message
        objJSON.insert(clsJSON::mscszMsgID, QString::number(++clsMsgSender::msulnglngMsgID));
        //Insert entry into the map
        clsMsgSender::msmpAcks.insert(std::make_pair(clsMsgSender::msulnglngMsgID, pAckMsgTrkr));
        //Start the timer that will resend message if ack. not received
        pAckMsgTrkr->pTimer->start(clsMsgSender::mscuint16AckTimeout);
        

        The value of 'mscuint16AckTimeout' is 5000 (milliseconds). I've also tried:

        pAckMsgTrkr->pTimer->start(std::chrono::milliseconds(clsMsgSender::mscuint16AckTimeout));
        

        I have a break point in the lambda slot, it doesn't get hit.

        K Offline
        K Offline
        KroMignon
        wrote on 16 Dec 2020, 08:15 last edited by KroMignon
        #8

        @SPlatten said in Timer with Lambda function:

        I have a break point in the lambda slot, it doesn't get hit.

        There are only 2 things why slot is not called:

        • the thread in which the QTimer in living does not have a running event loop
        • the event loop is locked (by a QThread::sleep() or a forever loop)

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SPlatten
          wrote on 16 Dec 2020, 08:27 last edited by
          #9

          I've re-written the code instead of a structure:

              class clsMsgTrkr : QTimer {
              private:
                  static const quint16 mscuint16AckTimeout = 5000;
                  static mmpAck msmpAcks;
          
                  QJsonObject mobjMsg;
                  clsMsgSender* mpMsgSndr;
          
              public:
                  clsMsgTrkr(clsMsgSender* pMsgSndr, const QJsonObject& crobjJSON) {
                      setInterval(mscuint16AckTimeout);
                      mobjMsg = crobjJSON;
                      mpMsgSndr = pMsgSndr;
              //Add tracker to list
                      clsMsgTrkr::msmpAcks.insert(std::make_pair(clsMsgSender::ulnglngGetMsgID(), this));
          
                      QObject::connect(this, &QTimer::timeout, [this]() {
                          qdbg() << "TIMEOUT!";
                          //Re-send message
                          emit mpMsgSndr->write(mobjMsg);
                      });
                      start();
                  }
              };
          

          I can see in the debugger the constructor is getting called and processed the timer is started but I don't get anything in the slot.

          Kind Regards,
          Sy

          J J 2 Replies Last reply 16 Dec 2020, 08:31
          0
          • S SPlatten
            16 Dec 2020, 08:27

            I've re-written the code instead of a structure:

                class clsMsgTrkr : QTimer {
                private:
                    static const quint16 mscuint16AckTimeout = 5000;
                    static mmpAck msmpAcks;
            
                    QJsonObject mobjMsg;
                    clsMsgSender* mpMsgSndr;
            
                public:
                    clsMsgTrkr(clsMsgSender* pMsgSndr, const QJsonObject& crobjJSON) {
                        setInterval(mscuint16AckTimeout);
                        mobjMsg = crobjJSON;
                        mpMsgSndr = pMsgSndr;
                //Add tracker to list
                        clsMsgTrkr::msmpAcks.insert(std::make_pair(clsMsgSender::ulnglngGetMsgID(), this));
            
                        QObject::connect(this, &QTimer::timeout, [this]() {
                            qdbg() << "TIMEOUT!";
                            //Re-send message
                            emit mpMsgSndr->write(mobjMsg);
                        });
                        start();
                    }
                };
            

            I can see in the debugger the constructor is getting called and processed the timer is started but I don't get anything in the slot.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 16 Dec 2020, 08:31 last edited by
            #10

            @SPlatten Don't know why you are now subclassing QTimer...
            You can add a destrcutor with debug output to see whether the instance is destroyed before timeout occurs.
            Also check what others wrote.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • S SPlatten
              16 Dec 2020, 08:27

              I've re-written the code instead of a structure:

                  class clsMsgTrkr : QTimer {
                  private:
                      static const quint16 mscuint16AckTimeout = 5000;
                      static mmpAck msmpAcks;
              
                      QJsonObject mobjMsg;
                      clsMsgSender* mpMsgSndr;
              
                  public:
                      clsMsgTrkr(clsMsgSender* pMsgSndr, const QJsonObject& crobjJSON) {
                          setInterval(mscuint16AckTimeout);
                          mobjMsg = crobjJSON;
                          mpMsgSndr = pMsgSndr;
                  //Add tracker to list
                          clsMsgTrkr::msmpAcks.insert(std::make_pair(clsMsgSender::ulnglngGetMsgID(), this));
              
                          QObject::connect(this, &QTimer::timeout, [this]() {
                              qdbg() << "TIMEOUT!";
                              //Re-send message
                              emit mpMsgSndr->write(mobjMsg);
                          });
                          start();
                      }
                  };
              

              I can see in the debugger the constructor is getting called and processed the timer is started but I don't get anything in the slot.

              J Offline
              J Offline
              JonB
              wrote on 16 Dec 2020, 08:32 last edited by
              #11

              @SPlatten
              As @jsulm has just said. And as I suggested, put timeout down to 0 while you debug....

              S 1 Reply Last reply 16 Dec 2020, 08:33
              0
              • J JonB
                16 Dec 2020, 08:32

                @SPlatten
                As @jsulm has just said. And as I suggested, put timeout down to 0 while you debug....

                S Offline
                S Offline
                SPlatten
                wrote on 16 Dec 2020, 08:33 last edited by SPlatten
                #12

                I added a call after call:

                start();
                qdbg() << this->isActive();
                

                qdbg is just a macro I use which is:

                #define qdbg()      qDebug().noquote().nospace()
                

                In the Application Output I see true so the timer is active, but doesn't timeout. Also, I just set the timer interval to 0, still no change, no timeout signal occurs.

                Kind Regards,
                Sy

                K 1 Reply Last reply 16 Dec 2020, 08:45
                0
                • S SPlatten
                  16 Dec 2020, 08:33

                  I added a call after call:

                  start();
                  qdbg() << this->isActive();
                  

                  qdbg is just a macro I use which is:

                  #define qdbg()      qDebug().noquote().nospace()
                  

                  In the Application Output I see true so the timer is active, but doesn't timeout. Also, I just set the timer interval to 0, still no change, no timeout signal occurs.

                  K Offline
                  K Offline
                  KroMignon
                  wrote on 16 Dec 2020, 08:45 last edited by
                  #13

                  @SPlatten said in Timer with Lambda function:

                  In the Application Output I see true so the timer is active, but doesn't timeout. Also, I just set the timer interval to 0, still no change, no timeout signal occurs.

                  Do you have a forever loop in your code or do you use QThread::sleep()?
                  Are you sure QEventLoop of the used thread is working?

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  S 1 Reply Last reply 16 Dec 2020, 08:53
                  0
                  • K KroMignon
                    16 Dec 2020, 08:45

                    @SPlatten said in Timer with Lambda function:

                    In the Application Output I see true so the timer is active, but doesn't timeout. Also, I just set the timer interval to 0, still no change, no timeout signal occurs.

                    Do you have a forever loop in your code or do you use QThread::sleep()?
                    Are you sure QEventLoop of the used thread is working?

                    S Offline
                    S Offline
                    SPlatten
                    wrote on 16 Dec 2020, 08:53 last edited by
                    #14

                    @KroMignon, the message transmission is in a thread and there very short sleep in the thread loop:

                    void clsMsgSender::run() {    
                        QJsonObject objJSON;
                        while( blnAnythingToDo(objJSON) == true ) {
                        //Sleep to allow a small cap between transmission
                            QThread::usleep(100);
                        //Look for a module name in the message
                            QJsonObject::iterator itrFound = objJSON.find(clsJSON::mscszMsgType);
                    
                            if ( itrFound != objJSON.end() ) {
                                const QJsonValueRef crobjMsgType = itrFound.value();
                                QString strMsgType(crobjMsgType.toString());
                    
                                if ( strMsgType.compare(clsJSON::mscszAck) != 0 ) {
                        //Insert a unique message ID into the message
                                    objJSON.insert(clsJSON::mscszMsgID, QString::number(++clsMsgSender::msulnglngMsgID));
                        //Create entry to monitor status of this message
                                    new clsMsgTrkr(this, objJSON);
                                }
                            }
                        //Writes message to socket
                            emit write(objJSON);
                        }
                        emit queueEmpty();
                    }
                    

                    Kind Regards,
                    Sy

                    J K 2 Replies Last reply 16 Dec 2020, 08:56
                    0
                    • S SPlatten
                      16 Dec 2020, 08:53

                      @KroMignon, the message transmission is in a thread and there very short sleep in the thread loop:

                      void clsMsgSender::run() {    
                          QJsonObject objJSON;
                          while( blnAnythingToDo(objJSON) == true ) {
                          //Sleep to allow a small cap between transmission
                              QThread::usleep(100);
                          //Look for a module name in the message
                              QJsonObject::iterator itrFound = objJSON.find(clsJSON::mscszMsgType);
                      
                              if ( itrFound != objJSON.end() ) {
                                  const QJsonValueRef crobjMsgType = itrFound.value();
                                  QString strMsgType(crobjMsgType.toString());
                      
                                  if ( strMsgType.compare(clsJSON::mscszAck) != 0 ) {
                          //Insert a unique message ID into the message
                                      objJSON.insert(clsJSON::mscszMsgID, QString::number(++clsMsgSender::msulnglngMsgID));
                          //Create entry to monitor status of this message
                                      new clsMsgTrkr(this, objJSON);
                                  }
                              }
                          //Writes message to socket
                              emit write(objJSON);
                          }
                          emit queueEmpty();
                      }
                      
                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 16 Dec 2020, 08:56 last edited by
                      #15

                      @SPlatten You expect this to work?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      S 1 Reply Last reply 16 Dec 2020, 08:58
                      0
                      • J jsulm
                        16 Dec 2020, 08:56

                        @SPlatten You expect this to work?

                        S Offline
                        S Offline
                        SPlatten
                        wrote on 16 Dec 2020, 08:58 last edited by SPlatten
                        #16

                        @jsulm , ok, perhaps I'm to close to see the obvious, what have I done wrong? Its not complete yet, I haven't finished the code yet, but is there something that would explain why the timer isn't working?

                        Kind Regards,
                        Sy

                        1 Reply Last reply
                        0
                        • S SPlatten
                          16 Dec 2020, 08:53

                          @KroMignon, the message transmission is in a thread and there very short sleep in the thread loop:

                          void clsMsgSender::run() {    
                              QJsonObject objJSON;
                              while( blnAnythingToDo(objJSON) == true ) {
                              //Sleep to allow a small cap between transmission
                                  QThread::usleep(100);
                              //Look for a module name in the message
                                  QJsonObject::iterator itrFound = objJSON.find(clsJSON::mscszMsgType);
                          
                                  if ( itrFound != objJSON.end() ) {
                                      const QJsonValueRef crobjMsgType = itrFound.value();
                                      QString strMsgType(crobjMsgType.toString());
                          
                                      if ( strMsgType.compare(clsJSON::mscszAck) != 0 ) {
                              //Insert a unique message ID into the message
                                          objJSON.insert(clsJSON::mscszMsgID, QString::number(++clsMsgSender::msulnglngMsgID));
                              //Create entry to monitor status of this message
                                          new clsMsgTrkr(this, objJSON);
                                      }
                                  }
                              //Writes message to socket
                                  emit write(objJSON);
                              }
                              emit queueEmpty();
                          }
                          
                          K Offline
                          K Offline
                          KroMignon
                          wrote on 16 Dec 2020, 09:00 last edited by KroMignon
                          #17

                          @SPlatten I've told you many times to read basic Qt documentation.

                          this cannot work!!!

                          First, I suppose clsMsgSender is subclassing QThread. And you have create your own run() implementation. So there is no running QEventLoop. This means, all QObject which are running in this thread can NOT receive/emit signals.

                          Second, you have a forever loop, so even if there where a running QEventLoop, it will not be called!

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          S 1 Reply Last reply 16 Dec 2020, 09:03
                          1
                          • K KroMignon
                            16 Dec 2020, 09:00

                            @SPlatten I've told you many times to read basic Qt documentation.

                            this cannot work!!!

                            First, I suppose clsMsgSender is subclassing QThread. And you have create your own run() implementation. So there is no running QEventLoop. This means, all QObject which are running in this thread can NOT receive/emit signals.

                            Second, you have a forever loop, so even if there where a running QEventLoop, it will not be called!

                            S Offline
                            S Offline
                            SPlatten
                            wrote on 16 Dec 2020, 09:03 last edited by
                            #18

                            @KroMignon , I'm impatient, I know there is benefit from reading the documentation, I just don't want to stop what I'm doing to spend what would seem a long time to digest the documentation.

                            Kind Regards,
                            Sy

                            K J 2 Replies Last reply 16 Dec 2020, 09:06
                            0
                            • S SPlatten
                              16 Dec 2020, 09:03

                              @KroMignon , I'm impatient, I know there is benefit from reading the documentation, I just don't want to stop what I'm doing to spend what would seem a long time to digest the documentation.

                              K Offline
                              K Offline
                              KroMignon
                              wrote on 16 Dec 2020, 09:06 last edited by KroMignon
                              #19

                              @SPlatten said in Timer with Lambda function:

                              I'm impatient,

                              Perhaps you are impatient, but in fact you are losing days doing nonsense code which not working.
                              If that is the best way to work, I am pretty sure NO.

                              Reading this document would take you 1 or 2 hours, how many hours have you spend to create those non working code?

                              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                              1 Reply Last reply
                              2
                              • S SPlatten
                                16 Dec 2020, 09:03

                                @KroMignon , I'm impatient, I know there is benefit from reading the documentation, I just don't want to stop what I'm doing to spend what would seem a long time to digest the documentation.

                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 16 Dec 2020, 09:10 last edited by
                                #20

                                @SPlatten said in Timer with Lambda function:

                                I just don't want to stop what I'm doing to spend what would seem a long time to digest the documentation

                                So, instead you waste your time here?
                                I really don't get the logic...

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                1
                                • S Offline
                                  S Offline
                                  SPlatten
                                  wrote on 16 Dec 2020, 09:12 last edited by
                                  #21

                                  The logic is I was I was hoping this would be quicker...obviously wrong, no worries, I will read the documentation.

                                  Kind Regards,
                                  Sy

                                  K 1 Reply Last reply 16 Dec 2020, 09:19
                                  0
                                  • S SPlatten
                                    16 Dec 2020, 09:12

                                    The logic is I was I was hoping this would be quicker...obviously wrong, no worries, I will read the documentation.

                                    K Offline
                                    K Offline
                                    KroMignon
                                    wrote on 16 Dec 2020, 09:19 last edited by
                                    #22

                                    @SPlatten said in Timer with Lambda function:

                                    The logic is I was I was hoping this would be quicker...obviously wrong, no worries, I will read the documentation.

                                    The point is not to read all Qt documentation, but at least the basics to understand how Qt is working.
                                    There are very basic notion to know.
                                    This will help you to right build your software and to take advantage of Qt architecture.

                                    And, as Qt is a asynchronous framework, most of the time you don't have to create threads.
                                    One thing is very important, never look thread this also locks the QEventLoop and break major Qt feature ==> signals/slots handling.

                                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                    S 1 Reply Last reply 16 Dec 2020, 09:26
                                    1
                                    • K KroMignon
                                      16 Dec 2020, 09:19

                                      @SPlatten said in Timer with Lambda function:

                                      The logic is I was I was hoping this would be quicker...obviously wrong, no worries, I will read the documentation.

                                      The point is not to read all Qt documentation, but at least the basics to understand how Qt is working.
                                      There are very basic notion to know.
                                      This will help you to right build your software and to take advantage of Qt architecture.

                                      And, as Qt is a asynchronous framework, most of the time you don't have to create threads.
                                      One thing is very important, never look thread this also locks the QEventLoop and break major Qt feature ==> signals/slots handling.

                                      S Offline
                                      S Offline
                                      SPlatten
                                      wrote on 16 Dec 2020, 09:26 last edited by
                                      #23

                                      @KroMignon , thanks for the advice, I've modified the thread loop to:

                                      void clsMsgSender::run() {    
                                          QJsonObject objJSON;
                                          while( blnAnythingToDo(objJSON) == true ) {
                                          //Sleep to allow a small cap between transmission
                                              exec();
                                              QThread::usleep(100);
                                          //Look for a module name in the message
                                              QJsonObject::iterator itrFound = objJSON.find(clsJSON::mscszMsgType);
                                      
                                              if ( itrFound != objJSON.end() ) {
                                                  const QJsonValueRef crobjMsgType = itrFound.value();
                                                  QString strMsgType(crobjMsgType.toString());
                                      
                                                  if ( strMsgType.compare(clsJSON::mscszAck) != 0 ) {
                                          //Insert a unique message ID into the message
                                                      objJSON.insert(clsJSON::mscszMsgID, QString::number(++clsMsgSender::msulnglngMsgID));
                                          //Create entry to monitor status of this message
                                                      new clsMsgTrkr(this, objJSON);
                                                  }
                                              }
                                          //Writes message to socket
                                              emit write(objJSON);
                                          }
                                          emit queueEmpty();
                                      }
                                      

                                      I've also added a class to replace the original structure:

                                          class clsMsgTrkr {
                                          Q_OBJECT
                                      
                                          private:
                                              static const quint16 mscuint16AckTimeout = 5000;
                                              static mmpAck msmpAcks;
                                      
                                              QJsonObject mobjMsg;
                                              clsMsgSender* mpMsgSndr;
                                              QTimer mtmrMonitor;
                                      
                                          public:
                                              clsMsgTrkr(clsMsgSender* pMsgSndr, QJsonObject& robjJSON);
                                              ~clsMsgTrkr();
                                      
                                          signals:
                                              void startTiming();
                                      
                                          public slots:
                                              void onStartTiming();
                                              void onTimeout();
                                          };
                                      

                                      The implementation:

                                      //Static initialisation
                                      mmpAck clsMsgTrkr::msmpAcks;
                                      /**
                                       * @brief clsMsgTrkr - Class constructor
                                       * @param pMsgSndr : Pointer to message sending class
                                       * @param robjJSON : Reference to JSON message
                                       */
                                      clsMsgTrkr::clsMsgTrkr(clsMsgSender* pMsgSndr, QJsonObject& robjJSON) {
                                          mobjMsg = robjJSON;
                                          mpMsgSndr = pMsgSndr;
                                      //Add tracker to list
                                          clsMsgTrkr::msmpAcks.insert(std::make_pair(clsMsgSender::ulnglngGetMsgID(), this));
                                      }
                                      /**
                                       * @brief clsMsgTrkr::~clsMsgTrkr - Class desctructor
                                       */
                                      clsMsgTrkr::~clsMsgTrkr() {
                                          if ( mtmrMonitor.isActive() ) {
                                              mtmrMonitor.stop();
                                          }
                                      }
                                      /**
                                       * @brief clsMsgTrker::onStartTiming
                                       */
                                      void clsMsgTrkr::onStartTiming() {
                                          QObject::connect(&mtmrMonitor, &QTimer::timeout, this, &clsMsgTrkr::onTimeout);
                                          mtmrMonitor.start(1);//mscuint16AckTimeout);
                                      }
                                      /**
                                       * @brief clsMsgTrkr::onTimeout
                                       */
                                      void clsMsgTrkr::onTimeout() {
                                          qdbg() << "TIMEOUT!";
                                          //Re-send message
                                          emit mpMsgSndr->write(mobjMsg);
                                      }
                                      

                                      Its still a work in progress, but I'm struggling with the compile errors:

                                      ../clsMsgSender.cpp:242:14: note: in instantiation of function template specialization 'QObject::connect<void (QTimer::*)(QTimer::QPrivateSignal), void (clsMsgTrkr::*)()>' requested here
                                          QObject::connect(&mtmrMonitor, &QTimer::timeout, this, &clsMsgTrkr::onTimeout);
                                      

                                      Kind Regards,
                                      Sy

                                      J K 2 Replies Last reply 16 Dec 2020, 09:31
                                      0
                                      • S SPlatten
                                        16 Dec 2020, 09:26

                                        @KroMignon , thanks for the advice, I've modified the thread loop to:

                                        void clsMsgSender::run() {    
                                            QJsonObject objJSON;
                                            while( blnAnythingToDo(objJSON) == true ) {
                                            //Sleep to allow a small cap between transmission
                                                exec();
                                                QThread::usleep(100);
                                            //Look for a module name in the message
                                                QJsonObject::iterator itrFound = objJSON.find(clsJSON::mscszMsgType);
                                        
                                                if ( itrFound != objJSON.end() ) {
                                                    const QJsonValueRef crobjMsgType = itrFound.value();
                                                    QString strMsgType(crobjMsgType.toString());
                                        
                                                    if ( strMsgType.compare(clsJSON::mscszAck) != 0 ) {
                                            //Insert a unique message ID into the message
                                                        objJSON.insert(clsJSON::mscszMsgID, QString::number(++clsMsgSender::msulnglngMsgID));
                                            //Create entry to monitor status of this message
                                                        new clsMsgTrkr(this, objJSON);
                                                    }
                                                }
                                            //Writes message to socket
                                                emit write(objJSON);
                                            }
                                            emit queueEmpty();
                                        }
                                        

                                        I've also added a class to replace the original structure:

                                            class clsMsgTrkr {
                                            Q_OBJECT
                                        
                                            private:
                                                static const quint16 mscuint16AckTimeout = 5000;
                                                static mmpAck msmpAcks;
                                        
                                                QJsonObject mobjMsg;
                                                clsMsgSender* mpMsgSndr;
                                                QTimer mtmrMonitor;
                                        
                                            public:
                                                clsMsgTrkr(clsMsgSender* pMsgSndr, QJsonObject& robjJSON);
                                                ~clsMsgTrkr();
                                        
                                            signals:
                                                void startTiming();
                                        
                                            public slots:
                                                void onStartTiming();
                                                void onTimeout();
                                            };
                                        

                                        The implementation:

                                        //Static initialisation
                                        mmpAck clsMsgTrkr::msmpAcks;
                                        /**
                                         * @brief clsMsgTrkr - Class constructor
                                         * @param pMsgSndr : Pointer to message sending class
                                         * @param robjJSON : Reference to JSON message
                                         */
                                        clsMsgTrkr::clsMsgTrkr(clsMsgSender* pMsgSndr, QJsonObject& robjJSON) {
                                            mobjMsg = robjJSON;
                                            mpMsgSndr = pMsgSndr;
                                        //Add tracker to list
                                            clsMsgTrkr::msmpAcks.insert(std::make_pair(clsMsgSender::ulnglngGetMsgID(), this));
                                        }
                                        /**
                                         * @brief clsMsgTrkr::~clsMsgTrkr - Class desctructor
                                         */
                                        clsMsgTrkr::~clsMsgTrkr() {
                                            if ( mtmrMonitor.isActive() ) {
                                                mtmrMonitor.stop();
                                            }
                                        }
                                        /**
                                         * @brief clsMsgTrker::onStartTiming
                                         */
                                        void clsMsgTrkr::onStartTiming() {
                                            QObject::connect(&mtmrMonitor, &QTimer::timeout, this, &clsMsgTrkr::onTimeout);
                                            mtmrMonitor.start(1);//mscuint16AckTimeout);
                                        }
                                        /**
                                         * @brief clsMsgTrkr::onTimeout
                                         */
                                        void clsMsgTrkr::onTimeout() {
                                            qdbg() << "TIMEOUT!";
                                            //Re-send message
                                            emit mpMsgSndr->write(mobjMsg);
                                        }
                                        

                                        Its still a work in progress, but I'm struggling with the compile errors:

                                        ../clsMsgSender.cpp:242:14: note: in instantiation of function template specialization 'QObject::connect<void (QTimer::*)(QTimer::QPrivateSignal), void (clsMsgTrkr::*)()>' requested here
                                            QObject::connect(&mtmrMonitor, &QTimer::timeout, this, &clsMsgTrkr::onTimeout);
                                        
                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 16 Dec 2020, 09:31 last edited by JonB
                                        #24

                                        @SPlatten
                                        If I'm not mistaken, you have not pasted the line before (or maybe after) the one you quote, which says what the actual error is.....

                                        1 Reply Last reply
                                        0
                                        • S SPlatten
                                          16 Dec 2020, 09:26

                                          @KroMignon , thanks for the advice, I've modified the thread loop to:

                                          void clsMsgSender::run() {    
                                              QJsonObject objJSON;
                                              while( blnAnythingToDo(objJSON) == true ) {
                                              //Sleep to allow a small cap between transmission
                                                  exec();
                                                  QThread::usleep(100);
                                              //Look for a module name in the message
                                                  QJsonObject::iterator itrFound = objJSON.find(clsJSON::mscszMsgType);
                                          
                                                  if ( itrFound != objJSON.end() ) {
                                                      const QJsonValueRef crobjMsgType = itrFound.value();
                                                      QString strMsgType(crobjMsgType.toString());
                                          
                                                      if ( strMsgType.compare(clsJSON::mscszAck) != 0 ) {
                                              //Insert a unique message ID into the message
                                                          objJSON.insert(clsJSON::mscszMsgID, QString::number(++clsMsgSender::msulnglngMsgID));
                                              //Create entry to monitor status of this message
                                                          new clsMsgTrkr(this, objJSON);
                                                      }
                                                  }
                                              //Writes message to socket
                                                  emit write(objJSON);
                                              }
                                              emit queueEmpty();
                                          }
                                          

                                          I've also added a class to replace the original structure:

                                              class clsMsgTrkr {
                                              Q_OBJECT
                                          
                                              private:
                                                  static const quint16 mscuint16AckTimeout = 5000;
                                                  static mmpAck msmpAcks;
                                          
                                                  QJsonObject mobjMsg;
                                                  clsMsgSender* mpMsgSndr;
                                                  QTimer mtmrMonitor;
                                          
                                              public:
                                                  clsMsgTrkr(clsMsgSender* pMsgSndr, QJsonObject& robjJSON);
                                                  ~clsMsgTrkr();
                                          
                                              signals:
                                                  void startTiming();
                                          
                                              public slots:
                                                  void onStartTiming();
                                                  void onTimeout();
                                              };
                                          

                                          The implementation:

                                          //Static initialisation
                                          mmpAck clsMsgTrkr::msmpAcks;
                                          /**
                                           * @brief clsMsgTrkr - Class constructor
                                           * @param pMsgSndr : Pointer to message sending class
                                           * @param robjJSON : Reference to JSON message
                                           */
                                          clsMsgTrkr::clsMsgTrkr(clsMsgSender* pMsgSndr, QJsonObject& robjJSON) {
                                              mobjMsg = robjJSON;
                                              mpMsgSndr = pMsgSndr;
                                          //Add tracker to list
                                              clsMsgTrkr::msmpAcks.insert(std::make_pair(clsMsgSender::ulnglngGetMsgID(), this));
                                          }
                                          /**
                                           * @brief clsMsgTrkr::~clsMsgTrkr - Class desctructor
                                           */
                                          clsMsgTrkr::~clsMsgTrkr() {
                                              if ( mtmrMonitor.isActive() ) {
                                                  mtmrMonitor.stop();
                                              }
                                          }
                                          /**
                                           * @brief clsMsgTrker::onStartTiming
                                           */
                                          void clsMsgTrkr::onStartTiming() {
                                              QObject::connect(&mtmrMonitor, &QTimer::timeout, this, &clsMsgTrkr::onTimeout);
                                              mtmrMonitor.start(1);//mscuint16AckTimeout);
                                          }
                                          /**
                                           * @brief clsMsgTrkr::onTimeout
                                           */
                                          void clsMsgTrkr::onTimeout() {
                                              qdbg() << "TIMEOUT!";
                                              //Re-send message
                                              emit mpMsgSndr->write(mobjMsg);
                                          }
                                          

                                          Its still a work in progress, but I'm struggling with the compile errors:

                                          ../clsMsgSender.cpp:242:14: note: in instantiation of function template specialization 'QObject::connect<void (QTimer::*)(QTimer::QPrivateSignal), void (clsMsgTrkr::*)()>' requested here
                                              QObject::connect(&mtmrMonitor, &QTimer::timeout, this, &clsMsgTrkr::onTimeout);
                                          
                                          K Offline
                                          K Offline
                                          KroMignon
                                          wrote on 16 Dec 2020, 09:43 last edited by KroMignon
                                          #25

                                          @SPlatten said in Timer with Lambda function:

                                          class clsMsgTrkr {
                                          Q_OBJECT

                                          Does not made sense, do you mean?

                                          class clsMsgTrkr : public QObject 
                                          {
                                               Q_OBJECT
                                          ...
                                          };
                                          

                                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                          S 1 Reply Last reply 16 Dec 2020, 09:45
                                          2

                                          15/35

                                          16 Dec 2020, 08:56

                                          • Login

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