Results 1 to 9 of 9

Thread: Simulate mouse click

  1. #1
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Simulate mouse click

    Hi ,

    I'm trying to do an eye mouse controlled.
    I want to simulate the click when the eye is closed.

    I already have the cursor position. i'm having problems with the mouse click simulation.

    I've tried this:

    QTestEventList *eventos = new QTestEventList();
    eventos->addMouseClick(Qt::LeftButton, 0, QPoint(x, y), -1);
    eventos->simulate(this);

    and this:

    QTest::mouseClick(this, Qt::LeftButton, 0, QPoint(x, y), -1);

    and:

    QMouseEvent *p = new QMouseEvent(QEvent::MouseButtonPress, QPoint(x, y), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    QCoreApplication::sendEvent(this, p);
    QMouseEvent *r = new QMouseEvent(QEvent::MouseButtonRelease, QPoint(x, y), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    QCoreApplication::sendEvent(this, r);

    Nothing works. I think it's because of the receiver, it's a QMainWindow. The thing is, I don't know in which button the mouse is over.

    Thanks.
    Last edited by Merylin; 20th July 2011 at 18:08.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Simulate mouse click

    This code:
    Qt Code:
    1. QTestEventList *eventos = new QTestEventList();
    2. eventos->addMouseClick(Qt::LeftButton, 0, QPoint(x, y), -1);
    3. eventos->simulate(this);
    To copy to clipboard, switch view to plain text mode 
    is essentially correct, but if it gets called again, your 'eventos' variable will leak memory.
    You better use a member instead of a local variable.
    Nothing works.
    How do you know that?
    What should happen if a mouse is clicked in your widget?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simulate mouse click

    thanks for the reply,

    Quote Originally Posted by high_flyer View Post
    This code:
    is essentially correct, but if it gets called again, your 'eventos' variable will leak memory.
    You better use a member instead of a local variable.
    Didn't thought about it.

    Quote Originally Posted by high_flyer View Post
    How do you know that?
    What should happen if a mouse is clicked in your widget?
    There are buttons in the QMainWindow... The button should be clicked, because the mouse is over it when the event is sent.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Simulate mouse click

    Are you sure the x,y position is correct?
    You can also give the address of the button it self...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simulate mouse click

    Yes, i'm sure the position is correct (because the mouse is moving to where i'm looking at the screen). And the buttons get hovered.
    I could give the address of the button if i knew which button was.. I could see which button is hovered, but don't know how.

    If everything fails, i'll have to see if the cursor coordinates are at the first button, or second, or third, and so on.. It should work but it doesn't look pretty.

    But thanks.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Simulate mouse click

    Yes, i'm sure the position is correct (because the mouse is moving to where i'm looking at the screen).
    Yes, but you might be giving coordinates relative to the wrong widget to the event.
    Plot the x,y values out, and see what you get.

    I could see which button is hovered, but don't know how.
    One option is to install an event filter, and catch the enterEvent() for example, and from that the pointer to the button or what ever other widget is under the mouse.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. The following user says thank you to high_flyer for this useful post:

    Merylin (21st July 2011)

  8. #7
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simulate mouse click

    One option is to install an event filter, and catch the enterEvent() for example, and from that the pointer to the button or what ever other widget is under the mouse.
    Thank you high_flyer!
    I installed event filters and know i know which is the button (using the QEvent::HoverEnter type) !
    Works great.

  9. #8
    Join Date
    Aug 2011
    Location
    India
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Simulate mouse click

    hope this helps:

    include the header file "windows.h"
    and use mouse_event(5 param);

    here's what I did for Right Click:
    Qt Code:
    1. mouse_event(MOUSEEVENTF_RIGHTDOWN,100, 200, 0,0);
    2. mouse_event(MOUSEEVENTF_RIGHTUP, 100, 200, 0,0);
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    4
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Simulate mouse click

    Although posted a long time ago , it gives me many tips.Thanks all.

Similar Threads

  1. How to simulate QTouchEvent using mouse
    By dpatel in forum Qt Programming
    Replies: 5
    Last Post: 28th April 2016, 02:40
  2. how to simulate mouse click behavior?
    By hashb in forum Qt Programming
    Replies: 2
    Last Post: 31st August 2010, 08:53
  3. Replies: 4
    Last Post: 15th March 2010, 15:49
  4. mouse click in QGprahicsScene
    By Morea in forum Qt Programming
    Replies: 11
    Last Post: 21st December 2006, 09:21
  5. mouse click event
    By vijay anandh in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2006, 10:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.