Important: Please read the Qt Code of Conduct -

Class for replacing the mouse click

  • Dear Qt ers,

    Hi...I need your suggestion, is there any class or function to replace a mouse click if I already know the coordinate of the cursor?

    Thank you in advance.


  • In your function that grabs the coordinates, you can write something like
    @if (event->button() == Qt::LeftButton) doSomething();@

  • Thanks for replying my thread Sir.
    May be my question was not so clear because my language limitation. I'm sorry for that.

    I have a pair of coordinate (x,y), after that I used 'setPos' function in QCursor class to move my mouse cursor. It has already done, the cursor moved to the desired (x,y) successfully without touched a "physical mouse".
    After pointing this cursor to (x,y) position, I want to give a click action like a mouse do. Would you give some suggestion.


  • Just connect the mouse move event (which you have already achieved) to a slot that the user's mouse click would normally trigger. Instead of using clicked() as the signal, you can create a custom signal for the mouse move event. An example of creating custom signals can be found "here":

  • I think he wants to make the mouse click programmatically, not act on a mouse click event.

    I'm not aware of a platform independent way to do this.
    If you're restrained to Microsoft® Windows®: I remember using the WinAPI function mouse_event with parameter MOUSEEVENTF_LEFTDOWN/UP, back in my dark, miserable Microsoft® Windows® days.

  • I didn't say a mouse click event, I said mouse move. Why do users click with their mouse? To trigger something. So the simple solution is to to have that "thing" trigger on the event where the mouse's coordinates reach the coordinates he specified.

  • So when the coordinates are over a different application, how would you reach into that application to trigger that event a mouse click would trigger? I'm afraid it's not quite as easy as you think, thus just making the mouse perform a click and let the operating system and the other application handle the rest will be the easier way.

  • When did he say he is running multiple applications? I assume you mean "coordinates over a different WIDGET" - not application?

    According to the question, there is a static pair of coordinates (x, y), and he has a scenario where the user's mouse is forced to move to that (x, y). Now that the mouse is at the (x, y) position, he wants to emulate a mouse click. A mouse click sends a clicked() signal in Qt, so all he would need to do is replace that with a custom signal that gets sent when the mouse reaches that (x, y) location.

  • Thank you for every reply and some discussion about that. Actually I need an emulate of a mouse click because after that I can do emulate drag action too. So after "click" action I can move an object in the coordinate (x,y) to another location (a new x,y location) like a mouse drag action do. I hope i can move a rectangle without using a "physical mouse", but only using my coordinates.


  • So you are looking to do animation then - take a look at "Qt's animation framework documentation":

Log in to reply