how to pass arguments to QTimer::singleshot slot



  • 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



  • 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.



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



  • @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
    }
    


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



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



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


  • Qt Champions 2017

    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.



  • @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



  • @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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.