How to change font size of child QLabel widget from the groupBox
Asked Answered
L

3

9

How can use different font & size for the child Widgets in the GroupBox and the tittle for the 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 tired 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 thistitle1 = QtGui.QLabel("Sample Title") as a child widget i can change it by

 title1.setFont(QtGui.QFont('SansSerif', 10))
Lola answered 22/9, 2015 at 18:24 Comment(0)
L
12

While I was waiting for an answer I wanted to give it a 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)
Lola answered 22/9, 2015 at 19:9 Comment(1)
this doesn't change the fonts while ui setup already renderedElytron
Q
5

You were trying to invoke the method setFont() of an object of the class list (which hasn't this method), not of the QtGui.QLabel object.

You can use a list comprehension for a 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]

If you need more voltage labels you only have to modify the list voltages.

And then even:

[vbox.addWidget(self.Voltage_Label[i], i, 0) for i in range(len(self.Voltage_Label))]
Quadruplex answered 19/3, 2016 at 3:11 Comment(0)
U
2

also, you can try

font = QtGui.QFont("Times", 8, QtGui.QFont.Bold)
[label.setFont(font) for label in self.Voltage_Label]

if you are creating a font object every time you iterate over an item of self.Voltage_Label it will cost you some memory. so, therefore, you can share the same one with all the labels. whenever memory matters you can use this technique. but if you want to change the font on all label objects it won't change the font in other QLabel objects.

Uela answered 9/6, 2020 at 2:59 Comment(1)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.Scorpaenid

© 2022 - 2024 — McMap. All rights reserved.