Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Python Qt6 - change font size at runtime
Forum Updated to NodeBB v4.3 + New Features

Python Qt6 - change font size at runtime

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 2 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PythonQTMarlem
    wrote on 13 Aug 2022, 20:08 last edited by Chris Kawa
    #1

    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?

    J 1 Reply Last reply 14 Aug 2022, 05:45
    0
    • P PythonQTMarlem
      13 Aug 2022, 20:08

      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?

      J Offline
      J Offline
      JonB
      wrote on 14 Aug 2022, 05:45 last edited by
      #2

      @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 be app.setStyleSheet("QPushButton{font-size: 18pt;}"). I presume you are also aware that this line does not use the fontsize from your combobox.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        PythonQTMarlem
        wrote on 16 Aug 2022, 16:17 last edited by PythonQTMarlem
        #3

        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.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          PythonQTMarlem
          wrote on 16 Aug 2022, 17:44 last edited by PythonQTMarlem
          #4

          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.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            PythonQTMarlem
            wrote on 16 Aug 2022, 18:10 last edited by
            #5

            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?

            J 1 Reply Last reply 16 Aug 2022, 18:31
            0
            • P PythonQTMarlem
              16 Aug 2022, 18:10

              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?

              J Offline
              J Offline
              JonB
              wrote on 16 Aug 2022, 18:31 last edited by
              #6

              @PythonQTMarlem
              I don't understand: you said earlier

              self.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.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                PythonQTMarlem
                wrote on 16 Aug 2022, 18:45 last edited by
                #7

                @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 Reply Last reply
                0

                1/7

                13 Aug 2022, 20:08

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved