以下是我的设置:
我的QTableWidget
上有一个eventFilter
来处理mousePress
和mouseRelease
事件
我还有一个自定义类,用于QPushButton
处理mousePress
和mouseRelease
事件
但是,当我通过单击并按住按钮来触发mousePress事件时,QTableWidget
没有看到该事件
以防万一,这里是自定义QPushButton
类和QTableWidget
{
class Button(QPushButton):
key = None
def __init__(self, title, parent):
super().__init__(title, parent)
def mousePressEvent(self, e):
super().mousePressEvent(e)
if e.button() == Qt.LeftButton:
print('press')
class TableWidget(QTableWidget):
def __init__(self, rows, columns, parent=None):
QTableWidget.__init__(self, rows, columns, parent)
self._last_index = QPersistentModelIndex()
self.viewport().installEventFilter(self)
def eventFilter(self, source, event):
if event.type() == QEvent.MouseButtonRelease:
print("mouse release")
return QTableWidget.eventFilter(self, source, event)
我希望看到的是,当我按下自定义按钮,然后释放鼠标时,它会在控制台中打印“鼠标释放”
这不会发生
但当我单击表中除按钮以外的任何位置时,它都会成功打印
如果您希望我添加任何额外信息,请在评论中告诉我
说明:
正如我在this post:中已经指出的,小部件之间的鼠标事件处理从子部件到父部件,也就是说,如果子部件不接受事件(它不使用事件),那么它将把事件传递给父部件。可使用以下示例进行验证:
如果按下QPushButton,则不会调用QTableWidget的mouseReleaseEvent方法,因为QPushButton会使用它,这与按下QLabel时不同,因为它不会使用它,而QTableWidget会使用它
解决方案:
正如我在另一篇文章中指出的,一个可能的解决方案是对QWindow使用eventfilter,并通过验证是否在单元格上单击来进行过滤
相关问题 更多 >
编程相关推荐