How to change the font size of a child QLabel widget from groupBox

How to use different fonts and size for child widgets in GroupBox and titles for GroupBox in python

def panel(self):
    groupBox = QtGui.QGroupBox("voltage Monitor")
    groupBox.setFont(QtGui.QFont('SansSerif', 13))       # the title size is good
    ..

    self.Voltage_Label = []
    ..

    vbox = QtGui.QGridLayout()
    self.Voltage_Label.append(QtGui.QLabel("voltage1 ")) # i need to have diff Font & size for these 
    self.Voltage_Label.append(QtGui.QLabel("voltage2 "))   
    self.Voltage_Label.append(QtGui.QLabel("voltage3 ")) 
    ..
    vbox.addWidget(self.Voltage_Label[i], i, 0)  
    ..
    groupBox.setLayout(vbox)
    return groupBox

I'm tired of this

   self.Voltage_Label.setFont(QtGui.QFont('SansSerif', 10))

I get this error

    !! self.Voltage_Label.setFont(QtGui.QFont('SansSerif', 10))
AttributeError: 'list' object has no attribute 'setFont' !!

but for something like this title1 = QtGui.QLabel("Sample Title")as a child widget I can change it to

 title1.setFont(QtGui.QFont('SansSerif', 10))
+4
source share
2 answers

While I was waiting for an answer, I wanted to try and found this method / solution for my question:

self.Voltage_Label = []

self.Voltage_Label.append(QtGui.QLabel("voltage1 ")) # i need to have diff Font & size for these 
self.Voltage_Label.append(QtGui.QLabel("voltage2 "))   
self.Voltage_Label.append(QtGui.QLabel("voltage3 "))
.
.

for i in xrange(5):
        newfont = QtGui.QFont("Times", 8, QtGui.QFont.Bold) 
        self.Voltage_Label[i].setFont(newfont)
+7
source

You tried to call a method of setFont()a class object list(which does not have this method), and not an object QtGui.QLabel.

You can use list comprehension for better scalability and performance:

voltages = ["voltage1 ", "voltage2 ", "voltage3 "]

# Populates the list with QLabel objects
self.Voltage_Label = [QtGui.QLabel(x) for x in voltages]

# Invokes setFont() for each object
[x.setFont(QtGui.QFont("Times", 8, QtGui.QFont.Bold)) for x in self.Voltage_Label]

, voltages.

:

[vbox.addWidget(self.Voltage_Label[i], i, 0) for i in range(len(self.Voltage_Label))]
+3

Source: https://habr.com/ru/post/1608560/


All Articles