0

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • What Qt version are you using, and on what operating system? Also, are you absolutely sure about the text item requirement? As I'm able to reproduce the issue even without implementing the `mousePressEvent` on an old PyQt version, but not on a newer PySide2, and that could be due to some changes in the QMainWindow mouse handling, but should happen anyway, and independently from item insertion. – musicamante May 29 '21 at 14:12
  • @musicamante - Hi, My OS is window 10 and Qt version is pyqt 5.12.3 . Python version is 3.7.6. I just tried PySide2(5.13.2) and found same problem.. could you let me know your version of PySide2 and Python?? I'll try that again. i'm sure that other items(RectItem, ellipsItem, lineItem etc..) dont have same issue.... – ksw8538 Jun 01 '21 at 14:49

0 Answers0