Change properties of QLabel, QLineEdit, QPushButton - I'm looking for an elegant way to code this
-
Hello,
This code works, but it's a lot of typing:def schrift_aendern(self): fontsize = int(self.cbxschriftgroesse.currentText()) self.lbl_schriftgroessen.setStyleSheet('font-size: ' + str(fontsize)+'px') self.cbxschriftgroesse.setStyleSheet('font-size: ' + str(fontsize)+'px') self.lbl_vorname.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.edt_vorname.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.lbl_nachname.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.edt_nachname.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.lbl_telefonnummer.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.edt_telefonnummer.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.btn_daten_exportieren.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.btn_programm_beenden.setStyleSheet('font-size: ' + str(fontsize)+'px')
Question:
Is there a way to achieve the same with less code? -
Hi,
Yes, create one stylesheet and apply it to the widget that contains these.
-
Thank You.
I tried this:
QMainWindow.setStyleSheet('font-size: ' + str(fontsize)+'px')
or this:
QPushButton(self).setStyleSheet('font-size: ' + str(fontsize)+'px')
Everything doesn't work. Can you please give me a code example?
-
@PythonQTMarlem
QMainWindow
is a class reference, not an instance, andQPushButton(self)
at best creates a temporaryQPushButton
instance and then discards it. You need to callsetStyleSheet()
on actual widget instances, like your working example does. -
I don't know what I have to do!
thiswidget = QWidget(self) widget.setStyleSheet('font-size: ' + str(fontsize)+'px')
don't change anything
This crashed:self.font().setStyleSheet('font-size: ' + str(fontsize)+'px')
This don't change anything:
self.window().setStyleSheet('font-size: ' + str(fontsize)+'px')
I don't know what I have to do! please show me!
-
-
Thank you for your help.
The Toolbar (```
self.toolbar = self.addToolBar("PyQt6 Telefonnummer-Verwaltung")change the fontsize, but the Qlabels, QComboBox, QLineEdit, QPushButton don't change the fontsizse.
-
This contrived example the has a ui form with a label and a button on it works as expected, the font size changes on both the label and button.
import os from pathlib import Path import sys from PySide6.QtWidgets import QApplication, QWidget from PySide6.QtCore import QFile from PySide6.QtUiTools import QUiLoader class Widget(QWidget): def __init__(self): super(Widget, self).__init__() self.load_ui() def load_ui(self): loader = QUiLoader() path = os.fspath(Path(__file__).resolve().parent / "form.ui") ui_file = QFile(path) ui_file.open(QFile.ReadOnly) self.ui = loader.load(ui_file, self) ui_file.close() # change the button and the label self.setStyleSheet('font-size: 18px') self.ui.changeLabel.setText('New Text') # change style for just the button self.ui.pushButton.setStyleSheet('color: red') self.ui.pushButton.clicked.connect(self.theButtonWasClicked) def theButtonWasClicked(self): self.ui.changeLabel.setText('Clicked') if __name__ == "__main__": app = QApplication([]) widget = Widget() widget.show() sys.exit(app.exec())
-
thanks for your code load_ui() is called in the constructor. For me it's like this:
class FensterKlasse(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("PyQt6 Telefonnummer-Verwaltung") # Position Breite und Höhe des Fensters wird festgelegt # Mit setGeometry. Diese Methode benötigt folgende # Parameter: x,y Breite Höhe self.setGeometry(500, 300, 360, 250) self.setAccessibleDescription("Programmfenster für PyQt6 Telefonnummer-Verwaltung") self.toolbar = self.addToolBar("PyQt6 Telefonnummer-Verwaltung") self.lbl_schriftgroessen = QLabel('&Schriftgröße ', self) self.lbl_schriftgroessen.setStyleSheet("font-size: 20px") self.lbl_schriftgroessen.setAccessibleDescription('Beschriftung für Aufklappbare Liste Schriftgröße') schriftgroessen = ["10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30", "32", "34", "36", "38", "40"] schriftgroessen.sort() self.cbxschriftgroesse = QComboBox(self) self.cbxschriftgroesse.setStyleSheet('font-size: 20px') self.cbxschriftgroesse.setAccessibleDescription('Auswahlliste für Schriftgröße') self.cbxschriftgroesse.addItems(schriftgroessen) self.cbxschriftgroesse.setCurrentIndex(5) self.cbxschriftgroesse.currentIndexChanged.connect(self.schrift_aendern) self.toolbar.addWidget(self.lbl_schriftgroessen) self.toolbar.addWidget(self.cbxschriftgroesse) self.lbl_schriftgroessen.setBuddy(self.cbxschriftgroesse) self.edt_vorname = QLineEdit(self) self.edt_vorname.setStyleSheet('font-size: 20px') self.edt_vorname.setGeometry(120, 40, 160, 30) self.edt_vorname.setAccessibleDescription('Eingabefeld für Vorname') self.lbl_vorname = QLabel('&Vorname:', self) self.lbl_vorname.setStyleSheet("font-size: 20px") self.lbl_vorname.setGeometry(10, 40, 90, 20) self.lbl_vorname.setAccessibleDescription('Beschriftung für Eingabefeld Vorname') self.lbl_vorname.setBuddy(self.edt_vorname) self.edt_nachname = QLineEdit(self) self.edt_nachname.setStyleSheet('font-size: 20px') self.edt_nachname.setGeometry(120, 75, 160, 30) self.edt_nachname.setAccessibleDescription('Eingabefeld für Nachname') self.lbl_nachname = QLabel('&Nachname:', self) self.lbl_nachname.setStyleSheet('font-size: 20px') self.lbl_nachname.setGeometry(10, 75, 100, 20) self.lbl_nachname.setAccessibleDescription('Beschriftung für Eingabefeld Nachname') self.lbl_nachname.setBuddy(self.edt_nachname) self.edt_telefonnummer = QLineEdit(self) self.edt_telefonnummer.setStyleSheet("font-size: 20px") self.edt_telefonnummer.setGeometry(180, 110, 160, 30) self.edt_telefonnummer.setAccessibleDescription('Eingabefeld fürTelefonnmummer') self.lbl_telefonnummer = QLabel('&Telefonnmummer:', self) self.lbl_telefonnummer.setStyleSheet('font-size: 20px') self.lbl_telefonnummer.setGeometry(10, 110, 160, 20) self.lbl_telefonnummer.setAccessibleDescription('Beschriftung für Eingabefeld Telefonnmummer') self.lbl_telefonnummer.setBuddy(self.edt_telefonnummer) self.btn_daten_exportieren = QPushButton("&Daten exportieren", self) self.btn_daten_exportieren.setStyleSheet('font-size: 20px') self.btn_daten_exportieren.setGeometry(10, 160, 180, 30) self.btn_daten_exportieren.setAccessibleDescription('Daten exportieren') self.btn_daten_exportieren.setToolTip('Eingabefelder in CSV-Datei Schreiben') self.btn_daten_exportieren.setDefault(True) self.btn_daten_exportieren.clicked.connect(self.csv_export) self.btn_programm_beenden = QPushButton("&Ende", self) self.btn_programm_beenden.setStyleSheet('font-size: 20px') self.btn_programm_beenden.setGeometry(240, 160, 100, 30) self.btn_programm_beenden.setAccessibleDescription('Programm beenden') self.btn_programm_beenden.setToolTip('Programm beenden') self.btn_programm_beenden.setDefault(True) self.btn_programm_beenden.clicked.connect(self.programm_beeden) self.statusBar().showMessage("Status bereit") self.statusBar().setAccessibleDescription("Status bereit") def schrift_aendern(self): fontsize = int(self.cbxschriftgroesse.currentText()) self.setStyleSheet('font-size: ' + str(fontsize)+'px')
Can you spot a mistake on my part?
-
@PythonQTMarlem
If (as you do) you set the stylesheet on the top-level window after you have added all its child widgets, are you expecting it to then change the font size used in all its existing widgets? Maybe it does, but I wouldn't be surprised if that would not happen...? (Or, just maybe the widgets need polishing in this case? Or some other kind of forced update?) [Does an expert here know about this?]If it were me I would test this. For example, what happens if you add new widgets or change text etc. after your change?
load_ui() is called in the constructor
This could make a difference, as the widgets are not rendered yet, by the time they are the font is in place? Plus, in @mranger90's case, isn't he setting/changing widget texts after the new style-font-size change?
I might risk a 10 cent bet that something about this is your issue.....
-
It was an attempt. This works:
def schrift_aendern(self): fontsize = int(self.cbxschriftgroesse.currentText()) # self.setStyleSheet('font-size: ' + str(fontsize)+'px') self.cbxschriftgroesse.setStyleSheet('font-size: ' + str(fontsize)+'px') self.lbl_vorname.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.edt_vorname.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.lbl_nachname.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.edt_nachname.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.lbl_telefonnummer.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.edt_telefonnummer.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.btn_daten_exportieren.setStyleSheet('font-size: ' + str(fontsize) + 'px') self.btn_programm_beenden.setStyleSheet('font-size: ' + str(fontsize)+'px')
But I think there must be a more elegant solution!
-
@PythonQTMarlem
Because that is changing the stylesheet explicitly on each widget, so it knows to redraw, instead of on the parent, where you rely on inheritance, -
I understand you very well. My real question is, can I solve this with a loop or by changing the font size of QLineEdit?
I think my solution requires too much python code! -
@PythonQTMarlem
I still think you may be able to do it with some judicious update after you set it on the parent, just not sure what. If I think of it I might give it a play tomorrow.Meanwhile, even if you have to set the style on each widget explicitly as you do, you can still do that more "elegantly" (less code) than your multiple explicit statements.
-
If you only want it on certain widgets, you can create an array/list of those you want (
[self.cbxschriftgroesse, self.lbl_vorname, self.edt_vorname, ...]
) and then iterate that list callingsetStyleSheet()
on each element. -
If you want to set it on all widgets (probably indeed the case?) use
QObject.findChildren()
from the top-level widget to get a list of all descendant widgets and callsetStyleSheet()
on those. This works without any knowledge of what the actual descendants are.
-
-
@PythonQTMarlem
Well, you seem to have disappeared since my last post above!Anyway, I have had the time to knock up a Python example. I only use Qt5, this is PySide2. No Qt6. But the font size works immediately, propagating to child widgets, without needing to do anything. Here is my code:
import sys from PySide2.QtWidgets import * # from PyQt5.QtWidgets import * class Main(QWidget): def __init__(self): super().__init__() self.resize(200, 200) self.label = QLabel("Hello world") self.lineedit = QLineEdit("Line edit") self.combo = QComboBox() self.combo.addItems(["10", "14", "18", "22"]) self.combo.currentTextChanged.connect(self.changeFont) layout = QVBoxLayout(self) layout.addWidget(self.label) layout.addWidget(self.lineedit) layout.addWidget(self.combo) def changeFont(self, text): fontsize = int(text) #self.label.setStyleSheet('font-size: ' + str(fontsize)+'px') #self.lineedit.setStyleSheet('font-size: ' + str(fontsize)+'px') #self.combo.setStyleSheet('font-size: ' + str(fontsize)+'px') self.setStyleSheet('font-size: ' + str(fontsize)+'px') if __name__ == '__main__': app = QApplication(sys.argv) window = Main() window.show() sys.exit(app.exec_())
Just doing
self.setStyleSheet('font-size: ' + str(fontsize)+'px')
resizes the font in all the widgets, without needing the commented out lines.I suggest you try this with your Qt6/PySide6 and see how it behaves for you.