Results 1 to 6 of 6

Thread: How to make QT Mouse move event faster?

  1. #1
    Join Date
    Jun 2018
    Location
    Silicon Valley
    Posts
    6
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Question How to make QT Mouse move event faster?

    Hello!
    I am designing a ui that incorporates a cross hair like feature that tracks the mouse in the UI windows, but when I move the mouse quickly I get significant lag behind the cursor. I am wondering if anyone knows a way to increase the update/refresh rate on the mouseMoveEvent function. Thank you!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to make QT Mouse move event faster?

    What you are observing is likely due to excessive repainting in your widget. If have implemented your cursor such that each mouse move causes a repaint of the window content, then that's probably the cause of the lag.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    Yippiyak (16th July 2018)

  4. #3
    Join Date
    Jun 2018
    Location
    Silicon Valley
    Posts
    6
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to make QT Mouse move event faster?

    Quote Originally Posted by d_stranz View Post
    What you are observing is likely due to excessive repainting in your widget. If have implemented your cursor such that each mouse move causes a repaint of the window content, then that's probably the cause of the lag.
    So what do you suggest instead of repainting? I need the crosshairs to track the cursor flawlessly, so I am unsure what I need to fix in order to do so.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to make QT Mouse move event faster?

    You could try using a Qt::CrossCursor QCursor instead of your own crosshairs. You could also derive from QRubberBand and pay careful attention to how the QRubberBand::paintEvent() is implemented.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    Yippiyak (16th July 2018)

  7. #5
    Join Date
    Jun 2018
    Location
    Silicon Valley
    Posts
    6
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Question Re: How to make QT Mouse move event faster?

    Quote Originally Posted by d_stranz View Post
    You could also derive from QRubberBand and pay careful attention to how the QRubberBand::paintEvent() is implemented.
    Can you elaborate on how I would do such a thing? I do implement a rubberband for selecting/highlighting data, but perhaps I am in fact not paying enough attention to its implementation.

  8. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to make QT Mouse move event faster?

    What I have done to implement custom rubberbands is a little bit complex. My plotting widget supports many different rubber band types, from vertical or horizontal lines, to rectangles, to arbitrary lassos. There are also three modes of operation: zoom, pan, or select. These different modes can be controlled by the mouse or keyboard (or both). To handle all this, I have a large state machine (using the Qt state machine framework) which controls what happens in each mode, what gets painted, and what signals are emitted at the end of an interaction. It is very generic, meant to handle almost any type of interaction with a plot.

    What it does not support is having a crosshair cursor that is visible all the time. It does support a crosshair rubberband which is turned on by a mouse press, moved, and turned off by a mouse release.

    To give this real-time performance, I do essentially this:

    - when the mouse is pressed, I create a snapshot of the window by rendering it into a QPixmap. I also create a QPainterPath and put the mouse point into it.

    - when the mouse moves, I update the QPainterPath as appropriate for the style of rubber band. For example, for a horizontal line rubber band, I update the path to hold a line from the initial position to the current position while keeping the y coordinate fixed. I then invalidate the widget to cause an repaint.

    - in the widget's paint event, I check the state machine to see if the rubberband is active, and if so, I draw the pixmap (instead of the normal paint update for the widget), then I draw the painter path. If the rubberband is not active, I let the widget redraw itself as normal. This procedure of drawing the pixmap then drawing the path means the path gets erased and redrawn smoothly with each move and doesn't leave "mouse droppings" behind.

    - in the mouse release event, I emit a signal appropriate for the mode (eg. zoomTo( x0, y0, x1, y1 ) based on the start and end coordinates of the rubber band), destroy the pixmap, and invalidate the window.

    The net effect is that while the rubber band is active, the underlying QWidget is being redrawn from the pixmap, not its native painting code. This gives realtime performance since drawing a pixmap is instantaneous. As soon as the mouse interaction is over, the widget gets repainted using its normal code.

    Your plotting widget doesn't necessarily need to be rewritten to use this; you can install an event filter that tracks the paint and mouse events and overrides the widget's normal behaviour by vectoring them into the custom rubber band class. If the rubber band is active, the rubber band class eats the events and does what it needs. If the rubber band is not active, the event filter does nothing and lets the widget handle them as normal.

    But like I said, this doesn't work if you need dynamic updates to the widget while the mouse is moving.

    If the updates to the plot are not frequent and if you want to redesign your plot widget, you can move the pixmap code into the widget itself. Each time the plot data (or viewport onto the data) changes, you render the new plot to a pixmap and simply draw the pixmap in every paint event. This will give you realtime redraws under all circumstances, and the only time the actual drawing code gets executed is on a data, viewport, or size change.
    Last edited by d_stranz; 16th July 2018 at 23:16.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. The following user says thank you to d_stranz for this useful post:

    Yippiyak (16th July 2018)

Similar Threads

  1. catch (spaceBar+mouse move) event
    By prasad.ece2 in forum Qt Programming
    Replies: 4
    Last Post: 3rd December 2013, 04:24
  2. Problem with QGraphicsScene mouse move event
    By Nesbit in forum Qt Programming
    Replies: 2
    Last Post: 22nd April 2012, 19:12
  3. Replies: 3
    Last Post: 7th January 2012, 09:38
  4. How to make painting on mouse move?
    By athulms in forum Newbie
    Replies: 1
    Last Post: 3rd November 2011, 10:07
  5. Mouse Move Event
    By merry in forum Newbie
    Replies: 5
    Last Post: 3rd June 2007, 07:26

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.