Python 未能从以下扩展类获取mouseMoveEvent

Python 未能从以下扩展类获取mouseMoveEvent,python,pyqt,Python,Pyqt,运行代码时,我无法运行mouseMoveEvent事件。绘制矩形时,如果我右键单击,将执行“contextmenuEvent”,但在任何时候都不会执行“mouseMoveEvent”,这是我失败的地方。(使用Python 3和PyQt5编写代码) Pantalla.py班 import sys from PyQt5 import QtWidgets, QtCore, QtGui class Pantalla(QtWidgets.QGraphicsScene): def __init

运行代码时,我无法运行mouseMoveEvent事件。绘制矩形时,如果我右键单击,将执行“contextmenuEvent”,但在任何时候都不会执行“mouseMoveEvent”,这是我失败的地方。(使用Python 3和PyQt5编写代码)

Pantalla.py班

import sys

from PyQt5 import QtWidgets, QtCore, QtGui

class Pantalla(QtWidgets.QGraphicsScene):

    def __init__(self):
        self.n = 0

    @staticmethod
    def get_pantalla(uno):
        app = QtWidgets.QApplication(sys.argv)
        scene = QtWidgets.QGraphicsScene()
        view = QtWidgets.QGraphicsView(scene)
        view.resize(640, 480)

        it_rect = MyRectButton(QtCore.QRectF(0, 0, 100, 100), 50)
        scene.addItem(it_rect)

        view.show()
        print("UNO" + str(uno))
        sys.exit(app.exec_())

    def mouseMoveEvent(self, event):
        print("Evento mouseMoveEvent")


class MyRectButton(QtWidgets.QGraphicsRectItem):
    def __init__(self, rect, x):
        super(MyRectButton, self).__init__(rect)
        self.x = x
        print("XX: " + str(self.x))

    def contextMenuEvent(self, event):
        print("contextMenuEvent")
        print("X: " + str(self.x))

    def mouseMoveEvent(self, event):
        print("mouseMoveEvent")

要调用mousemoveent方法,必须在mousePressEvent方法中接受事件。另一方面,pantall类根本没有被使用,所以它被删除了

导入系统 从PyQt5导入QtWidgets、QtCore、QtGui def get_pantalla(uno): app=qtwidts.QApplication(sys.argv) scene=qtwidts.qgraphicscene() view=QtWidgets.QGraphicsView(场景) 查看。调整大小(640480) it_rect=MyRectButton(QtCore.QRectF(0,0,100,100),50) 场景.附加项(直接) view.show() 打印(“UNO{}”。格式(UNO)) sys.exit(app.exec_()) 类MyRectButton(QtWidgets.QGraphicsRectItem): 定义初始化(self,rect,x): 超级(MyRectButton,self)。\uuuu初始化(rect) self.x=x 打印(“XX:{}.format(self.x)) def contextMenuEvent(自身、事件): 超级(MyRectButton,self).contextMenuEvent(事件) 打印(“上下文菜单事件”) 打印(“X:{}.format(self.X)) def鼠标压力事件(自身、事件): 超级(MyRectButton,self).mousePressEvent(事件) 打印(“鼠标压力”) event.accept() def mouseMoveEvent(自身、事件): 超级(MyRectButton,self).mouseMoveEvent(事件) 打印(“mouseMoveEvent”) def mouseReleaseEvent(自身,事件): 超级(MyRectButton,self).mouseReleaseEvent(事件) 打印(“鼠标事件”) 如果名称=“\uuuuu main\uuuuuuuu”: 获得潘塔拉(1)
eyllanesc,非常感谢,正如你所说
import sys

from PyQt5 import QtWidgets, QtCore, QtGui

class Pantalla(QtWidgets.QGraphicsScene):

    def __init__(self):
        self.n = 0

    @staticmethod
    def get_pantalla(uno):
        app = QtWidgets.QApplication(sys.argv)
        scene = QtWidgets.QGraphicsScene()
        view = QtWidgets.QGraphicsView(scene)
        view.resize(640, 480)

        it_rect = MyRectButton(QtCore.QRectF(0, 0, 100, 100), 50)
        scene.addItem(it_rect)

        view.show()
        print("UNO" + str(uno))
        sys.exit(app.exec_())

    def mouseMoveEvent(self, event):
        print("Evento mouseMoveEvent")


class MyRectButton(QtWidgets.QGraphicsRectItem):
    def __init__(self, rect, x):
        super(MyRectButton, self).__init__(rect)
        self.x = x
        print("XX: " + str(self.x))

    def contextMenuEvent(self, event):
        print("contextMenuEvent")
        print("X: " + str(self.x))

    def mouseMoveEvent(self, event):
        print("mouseMoveEvent")