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. dynamic timer firing different jobs

dynamic timer firing different jobs

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 555 Views 1 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 Offline
    S Offline
    SherifOmran
    wrote on last edited by aha_1980
    #1

    I am trying to create a dynamic timer that fires different jobs
    my implementation is as follows. But in the fired job function i get always the fired job =2 ! I need someway to say take the current value of the counter and don't change it.

                for (int counter =0; counter <2; counter++) {
                timers[counter].singleShot(seconds*1000, [&](){
                    FireJob(counter);
                }
    
    
    void FireJob(int jobcmd)
    {
    qDebug() <<  jobcmd;
    }
    

    any idea?

    mrjjM 1 Reply Last reply
    0
    • S SherifOmran

      I am trying to create a dynamic timer that fires different jobs
      my implementation is as follows. But in the fired job function i get always the fired job =2 ! I need someway to say take the current value of the counter and don't change it.

                  for (int counter =0; counter <2; counter++) {
                  timers[counter].singleShot(seconds*1000, [&](){
                      FireJob(counter);
                  }
      
      
      void FireJob(int jobcmd)
      {
      qDebug() <<  jobcmd;
      }
      

      any idea?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @SherifOmran
      Hi
      Hmm yes you are capturing the counter by reference ( like a pointer) so its
      always the current (loop) value
      Try with [=] to capture it as a copy.

      1 Reply Last reply
      3
      • S Offline
        S Offline
        SherifOmran
        wrote on last edited by
        #3

        @mrjj thank you for your reply. Can you please show me how to use the code by copy, this is new to me.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          its just = instead of & in the []

          for (int counter =0; counter <2; counter++) {
                      timers[counter].singleShot(seconds*1000, [ = ](){
                          FireJob(counter);
                      }
          
          1 Reply Last reply
          2
          • S Offline
            S Offline
            SherifOmran
            wrote on last edited by
            #5

            thank you , i got it

            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