Results 1 to 3 of 3

Thread: Contain mouse movement within drawing window

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

    Default Contain mouse movement within drawing window

    Hello,

    I have a simple widget and I draw simple shapes on it. I am selecting, say, a rectange, and moving it (dragging it actually) to another position in the drawing window. The problem is that, while dragging, I can move the mouse outside the drawing window, which is undesirable.

    Is there a way to contain mouse movement within the drawing window? I guess some custom code needs to be placed in the mouseMoveEvent handler, but using what type of logic roughly?

    TIA,

    rdr

  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: Contain mouse movement within drawing window

    You can use QCursor in a mouseMoveEvent(), and check the location of the mouse pointer, and move it back if it comes to the edge of your widget.
    A better method probably would be to use QHoverEvent, where you can react to a HoverLeave event, to place the cursor back on the 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
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Contain mouse movement within drawing window

    Note that by resetting the cursor to stay within an area, you are violating the user's expectations concerning mouse behavior. This is the sort of UI decision that frustrates users to no end, and I would very strongly recommend against it.

    It would be better to stop moving the shapewhen the cursor leaves the bounding area; this is easily handled using mouse move events, and follows standard GUI behavior.

Similar Threads

  1. QTextEdit mouse movement filtering
    By been_1990 in forum Qt Programming
    Replies: 18
    Last Post: 13th March 2021, 09:15
  2. Dymamic Painting outside paintEvent on mouse movement in QT4
    By shivam.priyadarshi in forum Qt Programming
    Replies: 7
    Last Post: 30th June 2012, 07:07
  3. Forwrad & Backward mouse movement
    By sujan.dasmahapatra in forum Newbie
    Replies: 1
    Last Post: 28th October 2009, 06:54
  4. Mouse movement problem in QGraphicsView
    By zgulser in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2009, 07:01
  5. Game mouse movement
    By chaosgeorge in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2006, 23:41

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.