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

how to pass arguments to QTimer::singleshot slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 12.2k Views 4 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on 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

    Pablo J. RoginaP 1 Reply Last reply
    0
    • Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on 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 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

          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

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on 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
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on 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

            Pablo J. RoginaP 1 Reply Last reply
            4
            • VRoninV VRonin

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

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on 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

              VRoninV kshegunovK 2 Replies Last reply
              0
              • Pablo J. RoginaP Pablo J. Rogina

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

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on 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
                • Pablo J. RoginaP Pablo J. Rogina

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

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on 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 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

                    VRoninV 1 Reply Last reply
                    0
                    • Q Qt Enthusiast

                      @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

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on 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

                      • Login

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