1

我的目标是检测用户何时悬停或停止悬停在框架上,但是每当我尝试使用 eventFilter 检测时,都不会运行任何事件来显示这一点。hoverEnter、hoverLeave 和 hoverMouseMove 的事件 ID 是 127、128 和 129,但是如果您运行代码,您会发现它们只是没有出现。这是失败的代码:

import sys
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *


class MainApp(QWidget):
    def __init__(self):
        super().__init__()
              
        self.setWindowTitle("Test Window")
        self.resize(300, 200)
        
        self.outerLayout = QHBoxLayout()
        self.outerLayout.setContentsMargins(50, 50, 50, 50)
        
        self.frame = QFrame()
        self.frame.setStyleSheet("background-color: lightblue;")
        self.innerLayout = QHBoxLayout(self.frame)
        self.label = QLabel(self.frame)
        self.label.setText("Example Frame")

        self.innerLayout.addWidget(self.label)
        self.outerLayout.addWidget(self.frame)
        
        self.setLayout(self.outerLayout)
    
    def eventFilter(self, obj, event):
        if event.type() == 127:
            print("hovered")
        elif event.type() == 128:
            print("no longer hovered")
        elif event.type() == 129:
            print("hover move event")
        print(event.type())
        return True

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainApp()
    window.installEventFilter(window)
    window.show()
    sys.exit(app.exec())

我的最终目标是能够检测何时单击 QFrame。我在想我会尝试通过检查鼠标点击来做到这一点,如果鼠标悬停在框架上,则触发该功能。

4

1 回答 1

2

首先应该注意,clicked 不是一个事件,而是一个信号。当按钮接收到 MouseButtonRelease 事件时,会发出按钮单击信号。

在这个答案中,我将至少展示以下方法来实现 QFrame 中的 clicked 信号。

  • 覆盖 mouseReleaseEvent

    import sys
    
    from PyQt6.QtCore import pyqtSignal, pyqtSlot
    from PyQt6.QtWidgets import QApplication, QFrame, QHBoxLayout, QLabel, QWidget
    
    
    class Frame(QFrame):
        clicked = pyqtSignal()
    
        def mouseReleaseEvent(self, event):
            super().mouseReleaseEvent(event)
            self.clicked.emit()
    
    
    class MainApp(QWidget):
        def __init__(self):
            super().__init__()
    
            self.setWindowTitle("Test Window")
            self.resize(300, 200)
    
            self.outerLayout = QHBoxLayout(self)
            self.outerLayout.setContentsMargins(50, 50, 50, 50)
    
            self.frame = Frame()
            self.frame.setStyleSheet("background-color: lightblue;")
    
            self.label = QLabel(text="Example Frame")
    
            self.innerLayout = QHBoxLayout(self.frame)
            self.innerLayout.addWidget(self.label)
    
            self.outerLayout.addWidget(self.frame)
    
            self.frame.clicked.connect(self.handle_clicked)
    
        @pyqtSlot()
        def handle_clicked(self):
            print("frame clicked")
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = MainApp()
        window.show()
        sys.exit(app.exec())
    
  • 使用事件过滤器:

    import sys
    
    from PyQt6.QtCore import QEvent
    from PyQt6.QtWidgets import QApplication, QFrame, QHBoxLayout, QLabel, QWidget
    
    
    class MainApp(QWidget):
        def __init__(self):
            super().__init__()
    
            self.setWindowTitle("Test Window")
            self.resize(300, 200)
    
            self.outerLayout = QHBoxLayout(self)
            self.outerLayout.setContentsMargins(50, 50, 50, 50)
    
            self.frame = QFrame()
            self.frame.setStyleSheet("background-color: lightblue;")
    
            self.label = QLabel(text="Example Frame")
    
            self.innerLayout = QHBoxLayout(self.frame)
            self.innerLayout.addWidget(self.label)
    
            self.outerLayout.addWidget(self.frame)
    
            self.frame.installEventFilter(self)
            # for move mouse
            # self.frame.setMouseTracking(True)
    
        def eventFilter(self, obj, event):
            if obj is self.frame:
                if event.type() == QEvent.Type.MouseButtonPress:
                    print("press")
                # for move mouse
                # elif event.type() == QEvent.Type.MouseMove:
                #    print("move")
                elif event.type() == QEvent.Type.MouseButtonRelease:
                    print("released")
            return super().eventFilter(obj, event)
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = MainApp()
        window.show()
        sys.exit(app.exec())
    

O 尝试错误的很大一部分是,这样做window.installEventFilter(window)只是从窗口本身而不是从 QFrame 侦听事件。解决方案是将 QFrame 事件发送到类window.frame.installEventFilter(window)

另一方面,不要使用数字代码,而是使用枚举,因为它们更具可读性。

另一方面,对于鼠标事件,Qt::WA_Hover必须启用该属性(阅读文档以获取更多信息)

import sys

from PyQt6.QtCore import QEvent, Qt
from PyQt6.QtWidgets import QApplication, QFrame, QHBoxLayout, QLabel, QWidget


class MainApp(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Test Window")
        self.resize(300, 200)

        self.outerLayout = QHBoxLayout(self)
        self.outerLayout.setContentsMargins(50, 50, 50, 50)

        self.frame = QFrame()
        self.frame.setStyleSheet("background-color: lightblue;")

        self.label = QLabel(text="Example Frame")

        self.innerLayout = QHBoxLayout(self.frame)
        self.innerLayout.addWidget(self.label)

        self.outerLayout.addWidget(self.frame)

        self.frame.setAttribute(Qt.WidgetAttribute.WA_Hover)
        self.frame.installEventFilter(self)

    def eventFilter(self, obj, event):
        if obj is self.frame:
            if event.type() == QEvent.Type.HoverEnter:
                print("enter")
            elif event.type() == QEvent.Type.HoverMove:
                print("move")
            elif event.type() == QEvent.Type.HoverLeave:
                print("leave")
        return super().eventFilter(obj, event)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainApp()
    window.show()
    sys.exit(app.exec())
于 2021-05-26T01:31:12.167 回答