Python Qt6 - change font size at runtime
-
wrote on 13 Aug 2022, 20:08 last edited by Chris Kawa
Hello,
I tried this code:def change_font(self): print("Change font") fontsize = int(self.cbxfontsize.currentText()) app.setStyleSheet("""QPushButton{font-size: 18pt;}""")
The good news is my Python program doesn't crash. The bad news is, the code doesn't change the font size at runtime.
Question:
How does it work? -
Hello,
I tried this code:def change_font(self): print("Change font") fontsize = int(self.cbxfontsize.currentText()) app.setStyleSheet("""QPushButton{font-size: 18pt;}""")
The good news is my Python program doesn't crash. The bad news is, the code doesn't change the font size at runtime.
Question:
How does it work?wrote on 14 Aug 2022, 05:45 last edited by@PythonQTMarlem
I don't know whether it will address your issue, but: the embedded double-quotes you have are not supposed to be there, and might stop it taking effect. It should beapp.setStyleSheet("QPushButton{font-size: 18pt;}")
. I presume you are also aware that this line does not use thefontsize
from your combobox. -
wrote on 16 Aug 2022, 16:17 last edited by PythonQTMarlem
Thank you for your reply. Unfortunately that's not the solution. My goal is for the user to be able to select the font size from a combo box, but first I need to figure out how to change the font size in a Python Qt6 application at runtime in general.
-
wrote on 16 Aug 2022, 17:44 last edited by PythonQTMarlem
The following attempt doesn't work either:
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.btn_programm_beenden.setFont(QFont('Arial', 18))
No error message appears, but the font size is not changed either.
-
wrote on 16 Aug 2022, 18:10 last edited by
This works!
def schrift_aendern(self): print("Schrift ändern") fontsize = int(self.cbxschriftgroesse.currentText()) self.btn_programm_beenden.setStyleSheet('font-size: ' + str(fontsize)+'px')
Question:
Is there a smart solution how I can apply this (maybe with a loop) to all QPushButton, QLabel and QLineEdit? -
This works!
def schrift_aendern(self): print("Schrift ändern") fontsize = int(self.cbxschriftgroesse.currentText()) self.btn_programm_beenden.setStyleSheet('font-size: ' + str(fontsize)+'px')
Question:
Is there a smart solution how I can apply this (maybe with a loop) to all QPushButton, QLabel and QLineEdit?wrote on 16 Aug 2022, 18:31 last edited by@PythonQTMarlem
I don't understand: you said earlierself.btn_programm_beenden.setStyleSheet('font-size: 20px')
didn't work, and now you say
self.btn_programm_beenden.setStyleSheet('font-size: ' + str(fontsize)+'px')
does work.
-
wrote on 16 Aug 2022, 18:45 last edited by
@PythonQTMarlem said in Python Qt6 - change font size at runtime:
self.btn_programm_beenden.setStyleSheet('font-size: 20px')
This:
self.btn_programm_beenden.setStyleSheet('font-size: 20px')
was in the create function. was in the create function.
I tried to change the fontsize for the whole application:
app.setStyleSheet("""QPushButton{font-size: 18pt;}""")
it doesn't work no matter how i write it.
now I had the idea to change the font size for only one QPushbutton:fontsize = int(self.cbxschriftgroesse.currentText()) self.btn_programm_beenden.setStyleSheet('font-size: ' + str(fontsize)+'px')
this works at runtime!
1/7