prevent cursor from redrawing
-
I am trying to create a small box that follows the mouse cursor over an image to select a specific part of it.
The box is a subclassed QLabel with the flags set to:
Qt::CustomizeWindowHint|Qt::FramelessWindowHint|Qt::Tool
The background set to:
Qt::WA_TranslucentBackground.The mouseMoveEvent is used to move the box when the cursor is over the image widget. The enterEvent and leaveEvent detect this.
The mouseMoveEvent is also used to update the cursor coordinates displayed in the status bar. The leaveEvent is used to clear the status bar text.This works really great, as long as the box and the cursor do not overlap. The box and the statusbar text behave as expected..
However, because the box needs to be centred over the mouse hotspot, there is an overlap between the cursor and the box. This causes the box to flicker continually, and when the mouse is outside the image, the status bar text flickers as well, which indicates that even though the mouse cursor is not over the image, the widget must be receiving ongoing leaveEvents.
I have tried setting the cursor to Qt::BlankCursor, but this has no effect.
Any suggestions on how to prevent the flicker would be appreciated. Although I would prefer to still see the mouse pointer, I would happily have this hidden if that were the only solution.
Thanks
allenb -
Hi and welcome to devnet,
Which version of Qt are you using ?
On which OS ?
Can you provide a minimal compilable example that shows this behavior ? -
Hi SGaist, Thank you :)
I am using Qt 5.15.2I have attached a small example that contains three classes:
A mainwindow, a testwidget and a box. The testwidget and box are both subclasses of QLabel.
The mainwindow contains the testwidget, the testwidget captures mouse events which move the box to follow the mouse.
Even when the mouse is not moving, the box flickers continually.Oh, I tried to upload the file, but received the message that "I do not have enough privileges for this action"
It can be downloaded here:
https://www.dropbox.com/scl/fi/o493be8f997ti29p037rc/QtCursorExample.zip?rlkey=ck18a9zqzb2r9q3vb7ig2zqoj&st=gjlg1mpx&dl=0
Thanks,
fallen -
I am really tearing my hair out with this one. I put it aside for a while, hoping that a break from it would give me a fresh perspective, but I still can't figure out what is causing the problem.
As mentioned above, I have a small test program containing three classes:
A mainwindow, a testwidget and a box. The testwidget and box are both subclasses of QLabel.
The mainwindow contains the testwidget, the testwidget captures mouse events which move the box to follow the mouse.However, even when the mouse is not moved, the box is continually redrawn and flickers constantly.
I tried posting the sample code here, however, it was flagged as spam by Akismet. I will try to follow up this message with the code.
I am sure that I am missing something very obvious, but I just can't see what it is, so any advice would be greatly appreciated.
Thanks,
Allen -
OK,
THIS IS INCREDIBLY FRUSTRATING!
I tried to post just the contents of the header file between code tags. It contains three small class declarations, about 30 lines of code.
I hit the submit button and immediately received a message that I had to wait 600 seconds.
After patiently waiting for 10 minutes and hitting submit again, aargh ...... "Code content was flagged as spam by Akismet"!So I have no idea how this forum works or if it even does, it seems to me to be pointless having a programming forum that can't even accept the most basic bit of code.
`
-
What about rather than trying to have a widget follow the cursor, if you make a custom cursor? Might be possible, and IMHO, woudl be a cleaner solution.
-
Kent, thanks for the response. This was my first thought as well, using a pixmap:
QCursor cursor = QCursor(pixmap); setCursor(cursor);However, for this application, the cursor needs to be scalable so that it adjusts its size as the image being selected is zoomed in or out.
Scaling the pixmap did not give the results I was looking for. However, in the interim, I have managed to find a solution thanks to someone on Stack Overflow.