Results 1 to 3 of 3

Thread: Mouse Events across multiple neighbor widgets, possible?

  1. #1
    Join Date
    Jun 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1

    Default Mouse Events across multiple neighbor widgets, possible?

    I'm making a minesweeper type game. My approach was to create a Board class containing a QGridLayout of rows and columns of mine widgets (think 16x30 like windows minesweeper for example). MouseMoveEvents are easily handled by setting setMouseTracking to true in the mine widget, but I ran into trouble trying to handle mouse presses and releases. For example, if the player clicks and holds on one mine and moves the mouse, that mine object receives a mouse press and move event, but when the mouse moves on to other mines, the event data (edit: and object data) is still relative to the initial mine. I want to somehow end each event as it passes on to a new widget, and propagate the original event to the new widget.

    I handled this in a Rube Goldberg-esque fashion by computing the row and column of each widget from the initial object event's global position and the fixed size of each mine widget relative to the parent widget like so:
    Qt Code:
    1. int boardOriginX = parentWidget()->mapToGlobal( QPoint(0, 0) ).x() + Board::margins;
    2. int boardOriginY = parentWidget()->mapToGlobal( QPoint(0, 0) ).y() + Board::margins;
    3. int currentRow = (event->globalY() - boardOriginY) / Board::mineSize;
    4. int currentCol = (event->globalX() - boardOriginX) / Board::mineSize;
    To copy to clipboard, switch view to plain text mode 

    and then setting up a Board public function
    Qt Code:
    1. Mine* Board::getMine( int row, int column )
    To copy to clipboard, switch view to plain text mode 
    to get the current mine under the mouse in each event handler. But this has the sideeffect that I'm doing a lot of higher level game handling inside the mouse event handlers of a single mine widget.

    This seems really sloppy allowing public access to private data (lol) and involves a lot of annoying and convoluted bounds checking and behind the scenes tracking of changing margins and sizes and number of mines to get the calculations right. Tell me there's an easier way? I wish there was a QMouseEvent member function that returned the current widget under the mouse, but that'd be too easy amirite?
    Last edited by weevil; 10th June 2010 at 06:51.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Mouse Events across multiple neighbor widgets, possible?

    You can check the point where the mouse release event happens. If it's still on the original widget, proceed, if not, do nothing with the original widget, get the one on the new position and handle the release event of the new widget.

    However, I think you handle the minefield in the wrong way.
    I would use a graphics view and use the mouse events of the scene. This would be easier.

  3. #3
    Join Date
    Jun 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    1

    Default Re: Mouse Events across multiple neighbor widgets, possible?

    Quote Originally Posted by tbscope View Post
    You can check the point where the mouse release event happens. If it's still on the original widget, proceed, if not, do nothing with the original widget, get the one on the new position and handle the release event of the new widget.
    I'm already using the event data to check where mouse moves are during a mouse press as well as mouse releases are happening. The only difference is I'm faking mouse event handling in the neighboring widgets through the current one using parent get and set functions. How would I send a new corresponding mouse event to the next widget once I calculate I've moved outside the originating widget? And by that I mean doing so without having to resort to asking the parent what widget my mouse is looking at, then sending my own hand crafted move/press/release event, which seems just as annoying and troublesome as what I'm doing now.

    However, I think you handle the minefield in the wrong way.
    I would use a graphics view and use the mouse events of the scene. This would be easier.
    I've only briefly looked at the model/view architecture, I'm still pretty new to Qt. Could you briefly expand on how that would make this easier? Appreciate the comment.

Similar Threads

  1. Mouse Events
    By daviddoria in forum Qt Programming
    Replies: 6
    Last Post: 13th May 2008, 12:55
  2. mouse events
    By xyzt in forum Newbie
    Replies: 3
    Last Post: 23rd March 2008, 12:14
  3. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 07:13
  4. Mouse Events in a widget
    By Rayven in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2006, 15:07
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 20:25

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.