Question

there is a very custom QWidget "A" that I don't have the code for, I'm only able to receive its QWidget* pointer. And I have to receive all of "A"s events and process them, before "A" receives any (some kind of an event filter).

Now I already tried to put another very simple QWidget "B" on top of it, but I cannot get this "B" to be fully transparent (I need to keep "A" all visible).

The next idea was to put a QLayout "C" on top of "A", but this layout doesn't receive enough events (in fact: I put a qDebug() statement into "C"::event(QEvent*evt), which never got executed at all - so the layout receives no events what so ever).

Is there any way of getting "A"'s events right before "A" so I can preprocess them, probably acking some of the events and pushing others down to "A"?

P.S.: "A" does OpenGL painting, but it's not derived off QGLWidget. Any Widgets I put atop just covered all of "A", setting Attributes like "WA_PointOnScreen" or "WA_TranslucentBackground" have not yielded any usable results.

P.P.S.: The idea of using a QLayout (like QGridLayout) is good, because I need to put a few small widgets atop of "A", that may also occlude "A" (but they can be hidden as needed).

EDIT: I just tried "C"->installEventFilter("A") and implemented bool "C"::eventFilter(QObject* QEvent*) - at no avail - eventFilter is never executed...

EDIT: As I just didn't want to give up, I tried the QWidget-approach, again. I found the WidgetAttribute WA_DontShowOnScreen, and this helps a bit:

  • I get all kinds of mouse events
  • I get most windowing system events (paint and resize are there, at least)
  • I'm missing any keyboard events - not a surprise, as invisible widgets normally have nothing to do with the keyboard...
  • I'm missing enter/leave events - also not really a surprise, but why do I get mouse events, then?
Was it helpful?

Solution

So finally I've got it working:

  • subclass QLayout for your needs
  • find out if your QWidget "A" has any QLayout attached
    • if yes: your QLayout should be of same kind, so you can reattach "A"'s children to it
    • if not: great, just attach your custom layout "A"->setLayout(myLayout);
  • the subclassed myLayout needs some stuff to do the trick:
    • reimplemented eventFilter() function
    • "A"->installEventFilter(myLayout);
    • in the eventFilter:
    • if(event->type() == QEvent::Enter) grabKeyboard();
    • if(event->type() == QEvent::Leave) releaseKeyboard();
    • ... all that event management you'd like to implement ...

The key to get it working was my misunderstanding of the Qt-docs regarding "installEventFilter". The "filterobject" is the object that wants to get all events, the monitored object is the one we'd like to get any events from. Then installation of the filter works like (and thinking about it makes it perfectly logical): monitoredObject->installEventFilter(filterobject);.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top