finding TextItems automatically

19 views
Skip to first unread message

Bill Eaton

unread,
Jun 6, 2019, 2:47:47 PM6/6/19
to pyqtgraph
I'd like to be able to find all of the TextItems in a PlotWidget automatically.

I'm trying to write some resize code that will automatically change the font size of TextItems as my app's MainWindows resizes.

For my other widgets and for the PlotWidget, I use  findChildren  a lot :
    def resizeEvent(self, event):
       
# figure out scale factor
        sfh
= float(self.centralWidget().height())/float(self.centralWidget().minimumHeight())
        sfw
= float(self.centralWidget().width())/float(self.centralWidget().minimumWidth())
        sf
= min(sfh,sfw)
 
        font
= QFont("Tahoma", 10.)  # Tahoma seems to work well on old XP boxes
        font
.setPointSizeF(sf*10.)
       
       
for widget in self.findChildren((QtWidgets.QLineEdit, QtWidgets.QTextEdit
                                         
QtWidgets.QLabel )):
            widget
.setFont(font)

       
for widget in self.findChildren((PlotWidget)):
            fs
= int(10.*sf)
            labelStyle
= {'font-size': str(fs) + 'pt'}
            widget
.setLabel('left',   **labelStyle)
            widget
.setLabel('bottom', **labelStyle)
            widget
.getAxis("left").tickFont = font
            widget
.getAxis("left").setStyle(tickTextOffset = fs)
            widget
.getAxis("bottom").tickFont = font
            widget
.getAxis("bottom").setStyle(tickTextOffset = fs)
            widget
.getAxis("left").setWidth(fs*10)

However, I cannot figure out how to find TextItems, I tried adding  pyqtgraph.TextItem  and  QtWidgets.QGraphicsTextItem  to my tuple of widgets in self.findChildren, but neither worked.

I also tried search in self.plotMain (my PlotWidget):
    for widget in self.plotMain.findChildren((pyqtgraph.TextItem, QtWidgets.QGraphicsTextItem )):
            widget
.setFont(font)
but that didn't work either.

I also tried playing with listDataItems, but no joy.






Bill Eaton

unread,
Jun 10, 2019, 5:06:12 PM6/10/19
to pyqtgraph
I'm having some success in using  allChildItems()  of the plot widget, combined with isinstance:

 def resizeEvent(self, event):
       
# figure out scale factor
        sfh
= float(self.centralWidget().height())/float(self.centralWidget().minimumHeight())
        sfw
= float(self.centralWidget().width())/float(self.centralWidget().minimumWidth())
        sf
= min(sfh,sfw)
 
        font
= QFont("Tahoma", 10.)  # Tahoma seems to work well on old XP boxes
        font
.setPointSizeF(sf*10.)


       
for item in self.plotMain.allChildItems():
           
if isinstance(item, pyqtgraph.graphicsItems.TextItem.TextItem):
                item
.setFont(font)
Reply all
Reply to author
Forward
0 new messages