[Interest] Receive key presses when hovering over a specific widget

Andy asmaloney at gmail.com
Thu Jul 11 16:15:05 CEST 2019


Not sure if this fits your use case 100%, but might give you some ideas. I
did something similar for magnifying images in QLabels, but I don't have
your additional focus issue.

On the thing being magnified (QLabel-derived class holding the image):

- holds a pointer to the magnifier and sets itself as the target of the
- in the keyPressEvent I look for Qt::Key_M and tell the magnifier it's
active (which shows it)
- in the keyReleaseEvent event I tell the magnifier it's not active (which
hides it)
- (for your case you might need to use enter/leave events to intercept the
keyboard? or maybe QWidget::underMouse()?)

In my magnifier class (QWidget-derived):

- it draws itself using info from the target widget
- installs an event filter on the target
   - handles mouse move to update the location/contents
   - handles enter and leave events to show/hide itself by checking the
mouse location against the target widget using QCursor::pos() (note that
QWidget::underMouse() will not work because the mouse may be over the
- also need to set some flags & attributes:
   setWindowFlags( Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint );

   setAttribute( Qt::WA_ShowWithoutActivating );
   setAttribute( Qt::WA_TransparentForMouseEvents );

I found it pretty tricky to get working (and I still have a couple of
things I'm not happy with) but it's very close (shippable!).

Good luck!

Andy Maloney  //  https://asmaloney.com
twitter ~ @asmaloney <https://twitter.com/asmaloney>

On Thu, Jul 11, 2019 at 7:54 AM Murphy, Sean <smurphy at walbro.com> wrote:

> I'm trying to figure out the best way to receive key presses when the user
> hovers over a specific widget, and deliver that keypress information to the
> parent widget.
> Say I've got three types of widgets:
> 1. thumbnailWidget - displays a low resolution version of a full
> resolution image, essentially based on QLabel
> 2. imageLibraryWidget - displays 1-N image thumbnail widgets. This widget
> is based on QWidget. Also owns all the full resolution images used to
> create the thumbnails
> 3. magnifierWidget - popup widget that displays a full resolution subset
> of the thumbnail. Think of it as just a magnifying glass that you can put
> over a portion of the thumbnail
> Application hierarchy is as follows:
> - MainWindow
>     - multiple "other" widgets, some of which can receive keyboard focus
> (QLineEdits, QTextEdits, etc.)
>     - one or more imageLibraryWidgets
>         - multiple "other" widgets, some of which can receive keyboard
> focus
>         - one or more imageThumbnailWidgets
>         - one magnifierWidget
> So the way I want this to work is that when the user hovers over a
> thumbnail widget AND presses a specific key, the imageLibraryWidget that is
> the parent of that thumbnail is notified that the correct keypress has been
> received, and knows which thumbnail (and which pixel of that thumbnail) the
> cursor is over. It then will popup the magnifierWidget with the appropriate
> full resolution pixels. When the user releases the key, or moves the mouse
> so they are no longer over a thumbnail, the magnifier is hidden.
> I've got the mouse position portion taken care of, but I'm struggling to
> get the key press thing correct, especially given that other widgets
> (either child widgets of the imageLibraryWidget or children of MainWindow)
> might have the keyboard focus at the time the user moves the mouse over a
> thumbnail and presses the bound key. I still want the magnifier idea to
> work, ideally without changing the keyboard focus from the currently
> focused widget.
> So the following should work:
> 1. User starts to type something in a line edit
> 2. Before finishing what they're typing they decide they need to double
> check something in the related image
> 3. User move cursor over image, and presses the key bound to the magnifier
> functionality. Let's say the key is 'm'
> 4. While over the image, and they are holding down the 'm' key, the
> keypress should be consumed and not get entered into the line widget
> That's the ideal case, I can live with it if the user would lose keyboard
> focus in the line edit and has to click back in it after the above sequence
> if I have to.
> Thoughts about how to go about this? I think I might need some combination
> of event filters and QHoverEvents, but not sure of the right combo at the
> moment.
> Sean
> This message has been scanned for malware by Forcepoint.
> www.forcepoint.com
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190711/1bb66adc/attachment.html>

More information about the Interest mailing list