QLabel 双击事件可以通过继承 QLabel 类并重写 mouseDoubleClickEvent() 方法来实现。
下面是一个简单的示例:
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore import Qt
class DoubleClickLabel(QLabel):
def __init__(self, parent=None):
super(DoubleClickLabel, self).__init__(parent)
def mouseDoubleClickEvent(self, event):
if event.button() == Qt.LeftButton:
print("Left button double clicked")
elif event.button() == Qt.RightButton:
print("Right button double clicked")
if __name__ == '__main__':
app = QApplication([])
label = DoubleClickLabel("Double click me!")
label.show()
app.exec_()
在这个代码中,我们创建了一个名为 DoubleClickLabel 的类,并在其中重写了 mouseDoubleClickEvent() 方法。该方法检查了双击的按钮类型,并打印出相应的消息。
我们用这个新的标签类创建一个标签,并显示它。当我们在标签上双击鼠标按钮时,会打印出相应的消息。