0

How to let MouseEvent working?

I try to print mouse tracking to label x,y coordinate but always fail. I already using setMouseTracking(True), generate from QtDesigner ui to py.

code Below:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(640, 480)
        Form.setMouseTracking(True)
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(270, 190, 58, 15))
        self.label.setObjectName("label")
        self.label.setMouseTracking(True)
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "TextLabel"))

    def mouseMoveEvent(self, e):

        x = e.x()
        y = e.y()

        text = "x: {0},  y: {1}".format(x, y)
        self.label.setText(text)
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
黃智傑
  • 23
  • 4

1 Answers1

1

Ui_Form is not a widget, so it will not have the mouseMoveEvent method, as the PyQt docs point out you must create a class that inherits the appropriate widget, in this case QWidget, and use the interface provided by Qt Designer:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(640, 480)
        Form.setMouseTracking(True)
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(270, 190, 58, 15))
        self.label.setObjectName("label")
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "TextLabel"))

class Form(QtWidgets.QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.setupUi(self)
        self.setMouseTracking(True)

    def mouseMoveEvent(self, e):
        text = "x: {0},  y: {1}".format(e.x(), e.y())
        self.label.setText(text)
        self.label.adjustSize()
        super(Form, self).mouseMoveEvent(e)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Form()
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Is that same to QMainWindow()? Cause my program show the bug below (After I change as same as your code ): MainWindow.setToolButtonStyle(QtCore.Qt.ToolButtonTextOnly) AttributeError: 'MainWindow' object has no attribute 'setToolButtonStyle' – 黃智傑 Nov 28 '18 at 14:44
  • @黃智傑 You can not use QMainWindow, when you use Qt Designer you have the possibility to choose which widget, if you want to use QMainWindow then use the Main Window template https://imgur.com/a/aFDxtUv – eyllanesc Nov 28 '18 at 19:11
  • @黃智傑 please read https://stackoverflow.com/questions/46544780/qtdesigner-changes-will-be-lost-after-redesign-user-interface/46545116#46545116 – eyllanesc Nov 28 '18 at 19:28
  • Thanks your help !! I fixed it !! – 黃智傑 Dec 23 '18 at 17:22
  • Sorry,I'm a beginner. :p – 黃智傑 Dec 23 '18 at 17:38