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

Murphy, Sean smurphy at walbro.com
Thu Jul 11 16:35:44 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

Yeah, that's the part I'm struggling with at the moment, but I only started 
messing with this late yesterday

> 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 magnifier
> - 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)

So here you're using keyPressEvent()/keyReleaseEvent() on the label derived 
class correct? Just making sure I'm following which widget you're talking about

> - (for your case you might need to use enter/leave events to intercept the 
> keyboard? or maybe QWidget::underMouse()?)

I think I will need to do that, and I wasn't doing it yesterday. So what I tried 
yesterday was just overriding the label's keyPressEvent/keyReleaseEvent functions. 
That only working if I clicked on the custom label class first, at that point 
keyPressEvent/keyReleaseEvent was getting triggered, and I able to watch for the 
assigned key. But at application launch, if I didn't click on the label, instead just 
mousing over it and then pressing a key, the label never received press/release 
events.

>
> 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 magnifier)
> - also need to set some flags & attributes:
>   setWindowFlags( Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint );
>
>   setAttribute( Qt::WA_ShowWithoutActivating );
>   setAttribute( Qt::WA_TransparentForMouseEvents );

I'll check all that out. At the moment, my label class doesn't have access to the 
full data. I should come clean and say that my actual setup isn't exactly what 
I've posted here, I contrived an example that everyone could understand 
without getting bogged down in what I'm actually doing, but I think I should be 
able to massage the concept enough to make it work. For other reasons, I want 
to keep the full data set stored by the label's parent, but I could probably easily
just pass a pointer down to the label that gives it access to the portion of full data.

> 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!).

What other things are you still not happy with? I'll keep an eye out for them and 
if I encounter them too, and solve them, I can feed that back to you.

Thanks for the pointers!
Sean


This message has been scanned for malware by Forcepoint. www.forcepoint.com


More information about the Interest mailing list