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. how to pass arguments to QTimer::singleshot slot

how to pass arguments to QTimer::singleshot slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 11.7k Views
  • 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on 26 Mar 2018, 15:51 last edited by
    #1

    I can use QTimer as:
    QTimer::singleShot(1500, this, SLOT(crossCapture()));

    But, can this be used like:
    QTimer::singleShot(1500, this, SLOT(crossCapture(int,int)));

    Please not crossCapture will be executed in GUI main thread

    P 1 Reply Last reply 26 Mar 2018, 15:59
    0
    • G Offline
      G Offline
      Gojir4
      wrote on 26 Mar 2018, 15:57 last edited by
      #2

      No, you cannot.
      You have to find another way to give the arguments when the "timeout" occurs.

      I you are using threads, you can use dedicated signal-slots to communicate with you GUI.

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on 26 Mar 2018, 15:58 last edited by
        #3

        howabout using std::bind features of c++ , if you can guide a litthle bit

        1 Reply Last reply
        1
        • Q Qt Enthusiast
          26 Mar 2018, 15:51

          I can use QTimer as:
          QTimer::singleShot(1500, this, SLOT(crossCapture()));

          But, can this be used like:
          QTimer::singleShot(1500, this, SLOT(crossCapture(int,int)));

          Please not crossCapture will be executed in GUI main thread

          P Offline
          P Offline
          Pablo J. Rogina
          wrote on 26 Mar 2018, 15:59 last edited by Pablo J. Rogina
          #4

          @Qt-Enthusiast what are the two integers you expect? I guess singleShot() is not aware of what you'll need. So you may need an indirect call, some pseudo-code here:

          QTimer::singleShot(1500, this, SLOT(crossCapture()));
          ...
          void crossCapture() {
               int x = getMousePointerX();
               int y = getMousePointerY();
               crossCapture(x, y);
          }
          
          void crossCapture(int x, int y) {
              // do the actual task
          }
          

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 26 Mar 2018, 17:05 last edited by
            #5

            Assuming decltype(this)==MyClass*:
            QTimer::singleShot(1500, this, std::bind(&MyClass::crossCapture,this,88,11));

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            P 1 Reply Last reply 26 Mar 2018, 17:06
            4
            • V VRonin
              26 Mar 2018, 17:05

              Assuming decltype(this)==MyClass*:
              QTimer::singleShot(1500, this, std::bind(&MyClass::crossCapture,this,88,11));

              P Offline
              P Offline
              Pablo J. Rogina
              wrote on 26 Mar 2018, 17:06 last edited by
              #6

              @VRonin sorry for asking, but how would you write to sentence to work dynamically? (not having constant values)

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              V K 2 Replies Last reply 26 Mar 2018, 17:08
              0
              • P Pablo J. Rogina
                26 Mar 2018, 17:06

                @VRonin sorry for asking, but how would you write to sentence to work dynamically? (not having constant values)

                V Offline
                V Offline
                VRonin
                wrote on 26 Mar 2018, 17:08 last edited by
                #7

                @Pablo-J.-Rogina You can still use a lambda: QTimer::singleShot(1500, this, [this]()->void{crossCapture(getMousePointerX(),getMousePointerY());});

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                4
                • P Pablo J. Rogina
                  26 Mar 2018, 17:06

                  @VRonin sorry for asking, but how would you write to sentence to work dynamically? (not having constant values)

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 26 Mar 2018, 18:29 last edited by
                  #8

                  Or:

                  QTimer::singleShot(1500, this, std::bind(&MyClass::crossCapture, this, getMousePointerX(), getMousePointerY()));
                  

                  But there's a very distinct difference between what @VRonin wrote and the above line. So in general they are not going to behave the same.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  3
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on 27 Mar 2018, 03:59 last edited by Qt Enthusiast
                    #9

                    @kshegunov said in how to pass arguments to QTimer::singleshot slot:

                    QTimer::singleShot(1500, this, std::bind(&MyClass::crossCapture, this, getMousePointerX(), getMousePointerY()));

                    It is giving me compilation error

                    I will let you know in Detail

                    I have a a function name

                    class myClass{
                    public:
                    void F(int argc,const char* argv[],char* msg,bool &done);
                    };

                    void myClass::F(int argc,const char* argv[],char* msg,bool &done) {
                    / using argc and argv
                    msg = "Message"
                    done = true;
                    }

                    now I have to call the same using QTimer::single shot

                    std::function<void(int ,const char* ,char* ,bool &> f = std::bind(&myClass::F, this, _1,_2,_3,_4) ;

                    QTimer::singleshot(0,this,SLOT(f());

                    it is not working , can some one correct my code

                    V 1 Reply Last reply 27 Mar 2018, 07:08
                    0
                    • Q Qt Enthusiast
                      27 Mar 2018, 03:59

                      @kshegunov said in how to pass arguments to QTimer::singleshot slot:

                      QTimer::singleShot(1500, this, std::bind(&MyClass::crossCapture, this, getMousePointerX(), getMousePointerY()));

                      It is giving me compilation error

                      I will let you know in Detail

                      I have a a function name

                      class myClass{
                      public:
                      void F(int argc,const char* argv[],char* msg,bool &done);
                      };

                      void myClass::F(int argc,const char* argv[],char* msg,bool &done) {
                      / using argc and argv
                      msg = "Message"
                      done = true;
                      }

                      now I have to call the same using QTimer::single shot

                      std::function<void(int ,const char* ,char* ,bool &> f = std::bind(&myClass::F, this, _1,_2,_3,_4) ;

                      QTimer::singleshot(0,this,SLOT(f());

                      it is not working , can some one correct my code

                      V Offline
                      V Offline
                      VRonin
                      wrote on 27 Mar 2018, 07:08 last edited by VRonin
                      #10

                      @Qt-Enthusiast said in how to pass arguments to QTimer::singleshot slot:

                      msg = "Message"

                      This is assigning the address of a local constant so doesn't work

                      this,SLOT(f());

                      We never used SLOT above, why did you decide to add it? QTimer::singleshot(0,this,f);

                      _1,_2,_3,_4

                      What is supposed to fill those?

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      3

                      2/10

                      26 Mar 2018, 15:57

                      topic:navigator.unread, 8
                      • Login

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