PyQt5中的鼠标悬停事件>>

问题描述 投票:0回答:1

我想获得鼠标悬停在标签上的位置。我读了this,但是我的问题有所不同。我需要抓住鼠标位置而不将鼠标悬停在标签上,因此mouseMoveEvent无法帮助您]

这是我的代码:

class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.WindowGUI()
        self.level = "Image Not Loaded Yet"
        self.mouseIsClicked = False
        self.top = 90
        self.left = 90
        self.height = 1800
        self.width = 1800
        self.setGeometry(self.top, self.left, self.height, self.width)
        self.setWindowTitle("Manual Contact Andgle")
        self.setMouseTracking(True)
        mainWidget = QWidget(self)
        self.setCentralWidget(mainWidget)
        mainWidget.setLayout(self.finalVbox)

        self.show()

    def WindowGUI(self):
        self.finalVbox = QVBoxLayout()  # Final Layout
        self.mainHBox = QHBoxLayout()  # Hbox for picLable and Buttons
        self.mainVBox = QVBoxLayout()  # VBox for two Groupboxes
        self.lineVBox = QVBoxLayout()  # VBox For Line Drawing Buttons
        self.fileVBox = QVBoxLayout()  # VBox for file Loading and Saving Buttons
        self.lineGroupbox = QGroupBox("Drawing")  # GroupBox For Line Drawing Buttons
        self.fileGroupbox = QGroupBox("File")  # GroupBox for File Loading and Saving Buttons
        self.picLable = Label(self)  # Lable For showing the Image
        self.piclable_pixmap = QPixmap("loadImage.png")  # Setting Pixmap
        self.picLable.setPixmap(self.piclable_pixmap)  # setting pixmap to piclable
    def mouseMoveEvent(self, QMouseEvent):
        print(QMouseEvent.pos())

我想获得鼠标悬停在标签上的位置。我读了这篇,但是我的问题有所不同。我需要抓住鼠标位置而不将鼠标悬停在我的标签上,因此,...

python user-interface pyqt pyqt5
1个回答
0
投票

[如果要在不按下小部件的情况下检测鼠标的位置,则必须启用mouseTracking,这将在按下或不按下鼠标时调用mouseMoveEvent,如果要确认未按下,则必须使用按钮( )方法:

import sys

from PyQt5 import QtWidgets


class Label(QtWidgets.QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setMouseTracking(True)

    def mouseMoveEvent(self, event):
        if not event.buttons():
            print(event.pos())
        super().mouseMoveEvent(event)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = Label()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())
© www.soinside.com 2019 - 2024. All rights reserved.