Qt - selectively allow a click to fall through to a lower application window
Asked Answered
M

2

6

Is it possible to receive a mouse click even in a Qt application, evaluate it, and if necessary, let it fall through to whatever might happen to be below the Qt application window?

Note that Qt::WA_TransparentForMouseEvents doesn't facilitate evaluating the click before passing it through.

And since the click evaluation incorporates some dynamic logic, it is not applicable to set a static mask either, on top of this having a visual impact as well.

Ideally, I would like a way to selectively allow the mouse click to pass through the application window in a platform portable way, ideally from QML and without bringing in the widgets module, or at the very least, without involving digging into private C++ internal APIs.

Mythify answered 14/10, 2018 at 13:17 Comment(0)
A
2

Qt::WA_TransparentForMouseEvents is used to filter mouse events out. The name is a bit of a misnomer: it allows widgets that would otherwise consume mouse events, not to consume them. E.g. you could make a button not notice any mouse events. If you're writing a custom widget, there's never any need for this attribute, since it's up to you to inspect the mouse events and simply not handle them: they are automatically passed to the parent widget.

But all of this doesn't matter much, since the WA_ attributes are for widgets, and do nothing for windows. You want something else entirely: to make the window itself transparent for input. Thus, in QML:

window.flags = window.flags | Qt.WindowTransparentForInput
Absurd answered 15/10, 2018 at 5:22 Comment(1)
Expect that this makes the window and all its content incapable of receiving any event. I need to receive the events, and decide whether I want to let them fall through.Mythify
B
0

use QWidget.setMask to set the repsonsible area of window:

    def resizeEvent(self, event):
        geo = self.frameGeometry()
        path = QtGui.QPainterPath()
        path.addEllipse(0, 0, 300, 200)
        poly = path.toFillPolygon().toPolygon()
        reg = QtGui.QRegion(poly)
        print('resize event', geo, poly)
        self.setMask(reg)
Beefsteak answered 6/5, 2022 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.