Search code examples
pythonpython-3.xpyqtpyqt5pyqtgraph

TextItem added to Viewbox disappeare after a couple of seconds


I'm trying to use pyqtgraph to plot multiply items, but unfortunately when I try to add TextItem to a Viewbox, it shows only for a couple sec, than it disappeare. Anyone has an idea, how fix the TextItem permanently?

I realized, that if I move the item in the box it won't disappeare but for me it isn't a good solution :(.

Any useful help appreciated! Thanks!

Here is my code:

import PyQt5
from PyQt5 import *
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication
import pyqtgraph as pg


app = QApplication([])


win = pg.GraphicsWindow()
win.setWindowTitle('Senzor data:')
win.setGeometry(2, 50, 1916, 1005)
win_border = pg.mkPen({'color': "040", 'width': 3})  #m, y, k, w
win.ci.setBorder(win_border)
Layout_border = pg.mkPen({'color': "0DE", 'width': 1})
Layout = win.addLayout(border=Layout_border)
#Layout.setSpacing(0)

Layout.addLabel("<b>pulse number", row=0, col=1, rowspan=1, colspan=4)
Layout.nextRow()
Layout.addLabel('pulse', angle=-90, row=1, col=0, rowspan=4, colspan=1)

View5 = Layout.addViewBox(row=1, col=1, rowspan=4, colspan=4)
pp = pg.TextItem("place of the pulse counter", color=(200, 200, 200), border='c', fill='b', anchor=(0.5, 0.5))
pp.setFont(QFont("", 50, QFont.Bold))
View5.addItem(pp)


app.exec_()

Solution

  • By analyzing what the viewRange() method returns from the ViewBox, it is modified until [0,0,0,0] is generated, so that there is no space to draw the TextItem.

    A solution is to establish a fixed range with the help of the method setRange():

    View5 = Layout.addViewBox(row=1, col=1, rowspan=4, colspan=4)
    View5.setRange(QRectF(-50, -50, 100, 100))
    

    Another possible solution is to use the autoRange() method to auto scale and place the TextItem in the center position, ie 0.5, 0.5

    View5 = Layout.addViewBox(row=1, col=1, rowspan=4, colspan=4)
    View5.autoRange()
    pp = pg.TextItem("place of the pulse counter", color=(200, 200, 200), border='c', fill='b', anchor=(0.5, 0.5))
    pp.setFont(QFont("", 50, QFont.Bold))
    pp.setPos(.5, .5)
    View5.addItem(pp)