I discovered that when I add QGraphicsTextItem to Scene, MouseMoveEvent Triggered without the mouse button pressed. I checked QgraphicsView.hasMouseTracking() value and it was False in default.
It happens only when I add 'TextItem' to Scene and It doesn't happen when I add other items (RectItem, LineItem, etc.) Why does 'TextItem' only trigger this problem? I used Spyder4 and my test code is this.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPainter, QColor, QFont, QPen, QBrush, QPainterPath
from PyQt5.QtCore import Qt , QPointF
from PyQt5.QtWidgets import QGraphicsItem
import sys
global ui
class MyScene(QtWidgets.QGraphicsScene):
c=0
def mouseMoveEvent(self,e):
super().mouseMoveEvent(e)
print(ui.graphicsView.hasMouseTracking())
print('move event! ',self.c)
self.c+=1
def mouseReleaseEvent(self, e):
super().mouseReleaseEvent(e)
end=e.scenePos()
text=QtWidgets.QGraphicsTextItem('haha')
text.setPos(end.x(), end.y())
self.addItem(text)
self.update()
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setObjectName("MainWindow")
self.resize(800, 619)
self.centralwidget = QtWidgets.QWidget(self)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.verticalLayout.addWidget(self.graphicsView)
self.setCentralWidget(self.centralwidget)
self.scene = MyScene()
self.graphicsView.setScene(self.scene)
app = QtWidgets.QApplication(sys.argv)
ui = MyWindow()
ui.show()
sys.exit(app.exec_())