【发布时间】:2015-02-04 13:08:43
【问题描述】:
我想拦截来自放置在另一个小部件顶部的小部件的事件,而不使用它。
这是我到目前为止所得到的。单击重叠部分时,我希望它打印“Hello World”,但我得到的只是“Hello”。
import sys
from PyQt5 import QtWidgets
class Button(QtWidgets.QPushButton):
def mousePressEvent(self, event):
print self.text()
return super(Button, self).mousePressEvent(event)
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setFixedSize(200, 100)
button1 = Button("World", window)
button1.move(10, 10)
button2 = Button("Hello", window)
button2.move(50, 15)
window.show()
app.exec_()
这是 QML 中的一个示例,可以满足我的要求。
import QtQuick 2.0
Item {
id: root
width: 200; height: 100
MouseArea {
anchors.fill: parent
onPressed: console.log("World")
}
Rectangle {
color: "green"
width: 100; height: 50
x: 10; y: 10
MouseArea {
anchors.fill: parent
propagateComposedEvents: true
onPressed: {
console.log("Hello");
mouse.accepted = false;
}
}
}
}
单击窗口会生成“World”,而单击绿色矩形会生成“Hello”,然后是“World”。这里的关键是propagateComposedEvents
【问题讨论】:
-
嗨,我写信澄清一下,你有没有解决过这个问题,“从重叠的小部件传播事件”?我有一个类似的案例link
-
哦,不确定。不久前,我想可能是这样:stackoverflow.com/a/27417852/478949