Results 1 to 5 of 5

Thread: How to program a mouse event?

  1. #1
    Join Date
    Jun 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default How to program a mouse event?

    mouseEvent
    Hi, I need to program pool.

    Right now, I'd like to make a mouseEvent so that, when i click on the screen, i get the position of the mouse.
    I know to use event->x(); to get the xCoordinate, but I don't know how to impletement the clickEvent itself.

    I got:

    Qt Code:
    1. void mouseMoveEvent(QMouseEvent* event)
    2. {
    3. if(event->button())
    4. {
    5. xPos = event->x();
    6. yPos = event->y();
    7. }
    8. }
    9.  
    10. main
    11. {
    12. ...
    13. while(certain condition)
    14. {
    15. QMouseEvent *event;
    16. while(!(event->button()))
    17. {
    18. event;
    19. }
    20.  
    21. mouseMoveEvent(event);
    22. ...
    23. }
    To copy to clipboard, switch view to plain text mode 

    If I do this, it gives a segmentation fault.

    I got 0 experience with implementing a mouseclick, unfortunatly :/

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to program a mouse event?

    mouseMoveEvent, mousePressEvent, mouseReleaseEvent's are called automatically when you click on associated widget.

    If you want to create customized mouse press event and post it:

    QMouseEvent* mousePressEvent= new QMouseEvent(QEvent::MouseButtonPress, QCursor:: pos(), this->mapToGlobal( QCursor:: pos()), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    QCoreApplication:: postEvent(this, mousePressEvent);
    Note: postEvent() is an asynchronous call while sendEvent() is synchronous call.
    Thanks :-)

  3. #3
    Join Date
    Jun 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to program a mouse event?

    Quote Originally Posted by prasad_N View Post
    mouseMoveEvent, mousePressEvent, mouseReleaseEvent's are called automatically when you click on associated widget.

    If you want to create customized mouse press event and post it:



    Note: postEvent() is an asynchronous call while sendEvent() is synchronous call.
    Oh yes, this is (almost) what I need!

    Right now it gives the global position on my screen, how do I made it that it takes the relative position in a certain part of my 'QGraphicsScene scene' ?

    (So only in the green part of the pooltable: http://imagehosting.jumbee.net/file....4_21_02_30.png (sorry that I didn't crop image).

  4. #4
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to program a mouse event?

    Replace Qcursor:os() with your widget position and maptoGlobal(thatPosition) as next parameter.

    in your case :
    Qpoint point =yourWidget->geometry().center();
    Qpoint glbPos = yourWidget->maptoGlobal(point);

    sorry I could not type well as I'm replying from my phone.
    Last edited by prasad_N; 24th June 2015 at 21:06.
    Thanks :-)

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to program a mouse event?

    What exactly are you trying to do?

    React to mouse interaction of the user or pretend being the user?

    Cheers,
    _

Similar Threads

  1. Ogre+qt mouse event (add object with mouse problem)
    By rimie23 in forum Qt Programming
    Replies: 7
    Last Post: 24th April 2012, 10:49
  2. Replies: 14
    Last Post: 17th January 2012, 09:01
  3. Replies: 3
    Last Post: 7th January 2012, 08:38
  4. Mouse Event - source of event?
    By PowerPlate in forum Qt Programming
    Replies: 1
    Last Post: 27th October 2011, 09:46
  5. Replies: 3
    Last Post: 12th May 2010, 13:11

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.