Reputation: 1
While running the code, I can't get the mouseMoveEvent event to run. The rectangle is drawn, if I right click, the "contextmenuEvent" is executed, but at no time the "mouseMoveEvent" is executed, where I am failing. (Code with Python 3 and PyQt5)
from pruebas.Pantalla import Pantalla
pantalla = Pantalla()
uno = 1
pantalla.get_pantalla(uno)
Class 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")
Upvotes: 0
Views: 34
Reputation: 244162
For the mouseMoveEvent method to be invoked, the event had to be accepted in the mousePressEvent method. On the other hand, the pantall class is not used at all, so it was removed.
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
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{}".format(uno))
sys.exit(app.exec_())
class MyRectButton(QtWidgets.QGraphicsRectItem):
def __init__(self, rect, x):
super(MyRectButton, self).__init__(rect)
self.x = x
print("XX: {}".format(self.x))
def contextMenuEvent(self, event):
super(MyRectButton, self).contextMenuEvent(event)
print("contextMenuEvent")
print("X: {}".format(self.x))
def mousePressEvent(self, event):
super(MyRectButton, self).mousePressEvent(event)
print("mousePressEvent")
event.accept()
def mouseMoveEvent(self, event):
super(MyRectButton, self).mouseMoveEvent(event)
print("mouseMoveEvent")
def mouseReleaseEvent(self, event):
super(MyRectButton, self).mouseReleaseEvent(event)
print("mouseReleaseEvent")
if __name__ == "__main__":
get_pantalla(1)
Upvotes: 1