Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I customize <h1> header style for a QWidget
Forum Update on Monday, May 27th 2025

How do I customize <h1> header style for a QWidget

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 2.0k 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.
  • R Offline
    R Offline
    Rory_1
    wrote on last edited by
    #1

    I am trying to make the font size of my app scalable. Almost everything is working. I am defining a font size via user input (fontPixelSize) and then combining it with a css QString (cssBase) that has the formatting for all the widgets I am using. Here is the relevant snippet:

    G::fontSize = QString::number(fontPixelSize);
    css = "QWidget {font-size: " + G::fontSize + "px;}" + cssBase;
    this->setStyleSheet(css);
    

    I want to define some html tags such as <h1> like this:

    h1 {
        font-size: 150%;
    }
    

    How do I set it up so this is the default for QWidget?

    Thanks in advance!

    JonBJ 1 Reply Last reply
    0
    • R Rory_1

      I am trying to make the font size of my app scalable. Almost everything is working. I am defining a font size via user input (fontPixelSize) and then combining it with a css QString (cssBase) that has the formatting for all the widgets I am using. Here is the relevant snippet:

      G::fontSize = QString::number(fontPixelSize);
      css = "QWidget {font-size: " + G::fontSize + "px;}" + cssBase;
      this->setStyleSheet(css);
      

      I want to define some html tags such as <h1> like this:

      h1 {
          font-size: 150%;
      }
      

      How do I set it up so this is the default for QWidget?

      Thanks in advance!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @rory_1
      What do you mean by "<h1> for a QWidget"? You can just include the HTML you have in the overall app stylesheet and any <h1> will respect it, I can only think of an <h1> appearing in a QTextEdit.

      R 1 Reply Last reply
      0
      • JonBJ JonB

        @rory_1
        What do you mean by "<h1> for a QWidget"? You can just include the HTML you have in the overall app stylesheet and any <h1> will respect it, I can only think of an <h1> appearing in a QTextEdit.

        R Offline
        R Offline
        Rory_1
        wrote on last edited by
        #3

        @jonb Thanks for the reply. The code I posted above is not working in a QLabel with rich text. However the font-size is working, which has been set at the QWidget level and is inherited by QLabel.

        JonBJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Just in case Qt's stylesheet system is for customising widgets. It's not a full blown CSS engine that you can use to manipulate the html content of QLabel like you would for a website.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          3
          • R Rory_1

            @jonb Thanks for the reply. The code I posted above is not working in a QLabel with rich text. However the font-size is working, which has been set at the QWidget level and is inherited by QLabel.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @rory_1
            So do you mean you have QLabel("<h1>Title</h1>"), intending it to render the font? Why not just say so?!

            As @SGaist has said, QSS is only a subset, you have to read https://doc.qt.io/qt-5/richtext-html-subset.html to see what is supported. And there I see

            h1 Level 1 heading Supports the standard block attributes.

            so it does not say it will respect font stuff?

            R 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Just in case Qt's stylesheet system is for customising widgets. It's not a full blown CSS engine that you can use to manipulate the html content of QLabel like you would for a website.

              R Offline
              R Offline
              Rory_1
              wrote on last edited by Rory_1
              #6

              @sgaist Thanks. That is what I thought but I was hoping I was wrong. Do you have any advice for how to make html in Widgets (ie QLabel) scalable?

              1 Reply Last reply
              0
              • JonBJ JonB

                @rory_1
                So do you mean you have QLabel("<h1>Title</h1>"), intending it to render the font? Why not just say so?!

                As @SGaist has said, QSS is only a subset, you have to read https://doc.qt.io/qt-5/richtext-html-subset.html to see what is supported. And there I see

                h1 Level 1 heading Supports the standard block attributes.

                so it does not say it will respect font stuff?

                R Offline
                R Offline
                Rory_1
                wrote on last edited by Rory_1
                #7

                @jonb I have tried referring to an external css file from the html in a QLabel->text.

                    h1 {
                        font-size: 5px;             /*does not work*/
                        color: red;                 /*works*/
                    }
                    
                    h2 {
                        color: green;               /*works*/
                        font-zize: 50px;            /*does not work*/
                    }
                    
                    body {
                      background-color: lightblue;  /*works*/
                      font-size: 100px;             /*works*/
                      color: blue;                  /*works*/
                    }
                

                All attributes except font size seem to work for the header tags. So, something appears to be over-riding the font-size for the header tags in Qt Widgets.

                JonBJ 1 Reply Last reply
                0
                • R Rory_1

                  @jonb I have tried referring to an external css file from the html in a QLabel->text.

                      h1 {
                          font-size: 5px;             /*does not work*/
                          color: red;                 /*works*/
                      }
                      
                      h2 {
                          color: green;               /*works*/
                          font-zize: 50px;            /*does not work*/
                      }
                      
                      body {
                        background-color: lightblue;  /*works*/
                        font-size: 100px;             /*works*/
                        color: blue;                  /*works*/
                      }
                  

                  All attributes except font size seem to work for the header tags. So, something appears to be over-riding the font-size for the header tags in Qt Widgets.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @rory_1
                  Have a read of https://stackoverflow.com/questions/932092/default-html-style-for-controls-in-the-qt-library , and the accepted answer.

                  What you want cannot be done with a QLabel. The QLabel is designed to hold primative text labels - it's HTML support is rather... ropey.

                  That was 10 years ago. My guess: it still applies ;-)

                  R M 2 Replies Last reply
                  1
                  • JonBJ JonB

                    @rory_1
                    Have a read of https://stackoverflow.com/questions/932092/default-html-style-for-controls-in-the-qt-library , and the accepted answer.

                    What you want cannot be done with a QLabel. The QLabel is designed to hold primative text labels - it's HTML support is rather... ropey.

                    That was 10 years ago. My guess: it still applies ;-)

                    R Offline
                    R Offline
                    Rory_1
                    wrote on last edited by
                    #9

                    @jonb Thanks for the link and your help. Much appreciated!!

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @rory_1
                      Have a read of https://stackoverflow.com/questions/932092/default-html-style-for-controls-in-the-qt-library , and the accepted answer.

                      What you want cannot be done with a QLabel. The QLabel is designed to hold primative text labels - it's HTML support is rather... ropey.

                      That was 10 years ago. My guess: it still applies ;-)

                      M Offline
                      M Offline
                      MMD18
                      wrote on last edited by
                      #10

                      @JonB said in How do I customize <h1> header style for a QWidget:

                      @rory_1
                      Have a read of https://stackoverflow.com/questions/932092/default-html-style-for-controls-in-the-qt-library , and the accepted answer.

                      What you want cannot be done with a QLabel. The QLabel is designed to hold primative text labels - it's HTML support is rather... ropey.

                      That was 10 years ago. My guess: it still applies ;-)

                      4 years later it is still actual :-)

                      SGaistS 1 Reply Last reply
                      0
                      • M MMD18

                        @JonB said in How do I customize <h1> header style for a QWidget:

                        @rory_1
                        Have a read of https://stackoverflow.com/questions/932092/default-html-style-for-controls-in-the-qt-library , and the accepted answer.

                        What you want cannot be done with a QLabel. The QLabel is designed to hold primative text labels - it's HTML support is rather... ropey.

                        That was 10 years ago. My guess: it still applies ;-)

                        4 years later it is still actual :-)

                        SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @MMD18 hi,

                        Because nothing has changed on that front for QLabel as it is not meant for complex html rendering. If you really need CSS styling, using WebEngine would be more indicated.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1

                        • Login

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