Search code examples
pythonpyqt5pyqtgraph

Dynamically rotate TextItem in pyqtgraph


I want to dynamically rotate TextItem but cannot get it to work. Changing position or anchor with setPos and setAnchor updates the item, but wanting to change angel with setAngle doesn't update the text. The strangest thing is that it does update once I drag the canvas.

from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg

app = QtGui.QApplication([])

w = pg.GraphicsView()
w.show()
w.resize(800,800)

view = pg.ViewBox()
w.setCentralItem(view)
view.setAspectLocked(True)
view.setRange(QtCore.QRectF(0, 0, 200, 200))

anchor = pg.TextItem()
anchor.setText("hey")
anchor.setColor(QtGui.QColor(255, 255, 255))

view.addItem(anchor)


def rotate():
    x, y = anchor.pos()
    anchor.setPos(x + 1, y + 1)
    anchor.setAngle(anchor.angle + 10)


timer = QtCore.QTimer()
timer.timeout.connect(rotate)
timer.start(1000)


if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

I am wondering what signal or function to call so that the item updates immediately.


Solution

  • It ended up being a bug. I solved it by just removing the old TextItem and creating a new one with the updatet angle.