PyQt5:检查enter-event中是否按下鼠标

时间:2017-09-11 02:06:48

标签: python pyqt mouseevent pyqt5 mouse-buttons

我的实际应用程序比这复杂得多,但下面的例子总结了我的大部分问题。我有多个QLabel,我已将其子类化以使其可点击。标签显示16x16图像,需要通过Pillow加载图像,将它们转换为ImageQt对象,然后设置标签的像素图。在该示例中,我有3个可单击的QLabel,每次单击它们时都会运行print_something函数。我的目标是能够按住鼠标,对于我悬停在其上的每个标签,函数都会被调用。任何指针都会很棒。

from PyQt5 import QtCore, QtWidgets, QtGui
from PIL import Image
from PIL.ImageQt import ImageQt
import sys


class ClickableLabel(QtWidgets.QLabel):

    def __init__(self):
        super().__init__()

    clicked = QtCore.pyqtSignal()

    def mousePressEvent(self, ev):
        if app.mouseButtons() & QtCore.Qt.LeftButton:
            self.clicked.emit()


class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()
        central_widget = QtWidgets.QWidget()

        self.setFixedSize(300, 300)
        image = Image.open("16x16image.png")
        image_imageqt = ImageQt(image)

        hbox = QtWidgets.QHBoxLayout()
        hbox.setSpacing(0)
        hbox.addStretch()

        label01 = ClickableLabel()
        label01.setPixmap(QtGui.QPixmap.fromImage(image_imageqt))
        label01.clicked.connect(self.print_something)
        hbox.addWidget(label01)

        label02 = ClickableLabel()
        label02.setPixmap(QtGui.QPixmap.fromImage(image_imageqt))
        label02.clicked.connect(self.print_something)
        hbox.addWidget(label02)

        label03 = ClickableLabel()
        label03.setPixmap(QtGui.QPixmap.fromImage(image_imageqt))
        label03.clicked.connect(self.print_something)
        hbox.addWidget(label03)

        hbox.addStretch()

        central_widget.setLayout(hbox)

        self.setCentralWidget(central_widget)

    def print_something(self):
        print("Printing something..")


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

QMouseEvent的文档中说明了问题的原因:

  

当内部按下鼠标按钮时,Qt会自动抓取鼠标   小部件;小部件将继续接收鼠标事件,直到   最后一个鼠标按钮被释放。

看起来并不是一个简单的方法,所以需要一些hackish。一个想法是启动假拖动,然后使用dragEnterEvent而不是enterEvent。这样的事情应该可行:

class ClickableLabel(QtWidgets.QLabel):
    clicked = QtCore.pyqtSignal()

    def __init__(self):
        super().__init__()
        self.setAcceptDrops(True)
        self.dragstart = None

    def mousePressEvent(self, event):
        if event.buttons() & QtCore.Qt.LeftButton:
            self.dragstart = event.pos()
            self.clicked.emit()

    def mouseReleaseEvent(self, event):
        self.dragstart = None

    def mouseMoveEvent(self, event):
        if (self.dragstart is not None and
            event.buttons() & QtCore.Qt.LeftButton and
            (event.pos() - self.dragstart).manhattanLength() >
             QtWidgets.qApp.startDragDistance()):
            self.dragstart = None
            drag = QtGui.QDrag(self)
            drag.setMimeData(QtCore.QMimeData())
            drag.exec_(QtCore.Qt.LinkAction)

    def dragEnterEvent(self, event):
        event.acceptProposedAction()
        if event.source() is not self:
            self.clicked.emit()