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. Maintaining Consistent Font Appearance in PySide6 QLabel: QSS vs. QFont

Maintaining Consistent Font Appearance in PySide6 QLabel: QSS vs. QFont

Scheduled Pinned Locked Moved Solved Qt for Python
qfontlabelqlabel
11 Posts 3 Posters 3.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.
  • EmrecpE Offline
    EmrecpE Offline
    Emrecp
    wrote on last edited by
    #1

    Hello,
    When I change the font property of a QLabel using QSS in PySide6, it appears as intended. However, when I try to do the same using QFont directly, it appears differently. I want the appearance to be consistent with the one achieved using QSS even when I use QFont. Is there a way to achieve this, or is it a problem?

    font.png

    Seeking to Achieve the Same Look as the First Label
    PySide6: 6.4.2
    Python: 3.11.2 x64

    Code:

    from PySide6.QtWidgets import *
    from PySide6.QtGui import *
    import sys
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        wid = QWidget(); lay = QVBoxLayout(wid)
        label = QLabel("HELLO! MY NAME IS EMRE")
        label.setStyleSheet("""font:'SF Pro Display';
        font-size:14pt;
        font-weight:900;""")
    
    
        label2 = QLabel("HELLO! MY NAME IS EMRE")
        font = QFont('SF Pro Display', 14)
        font.setWeight(QFont.Weight.Black) # 900
        label2.setFont(font)
    
    
        lay.addWidget(label)
        lay.addWidget(label2)
        wid.show()
        sys.exit(app.exec())
    
    
    JonBJ 1 Reply Last reply
    0
    • SGaistS SGaist moved this topic from General and Desktop on
    • EmrecpE Emrecp

      @SGaist I want to use this font in a paintEvent in my own project. Since I can't use stylesheet in paintEvent, I need how to get the same text with qfont. Just tell me the solution please

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

      @Emrecp
      I believe @SGaist is telling you that you cannot guarantee that a widget with a stylesheet will look identical to one you construct with coded style attributes. As soon as you place a stylesheet on a widget you are (potentially) losing some styles which are inbuilt, so it may differ.

      I suggested earlier you remove the weight stuff in your example and see whether just with the font in "normal" it does or does not look identical using the same font in both code and stylesheet.

      If you are coding inside paintEvent() then, as you say, you cannot use stylesheet. You can only set the attributes on a QFont as you have done.

      I don't know whether you can go through the sources of Qt to discover just what a current style like Fusion does.

      1 Reply Last reply
      2
      • EmrecpE Emrecp

        Hello,
        When I change the font property of a QLabel using QSS in PySide6, it appears as intended. However, when I try to do the same using QFont directly, it appears differently. I want the appearance to be consistent with the one achieved using QSS even when I use QFont. Is there a way to achieve this, or is it a problem?

        font.png

        Seeking to Achieve the Same Look as the First Label
        PySide6: 6.4.2
        Python: 3.11.2 x64

        Code:

        from PySide6.QtWidgets import *
        from PySide6.QtGui import *
        import sys
        
        if __name__ == '__main__':
            app = QApplication(sys.argv)
            wid = QWidget(); lay = QVBoxLayout(wid)
            label = QLabel("HELLO! MY NAME IS EMRE")
            label.setStyleSheet("""font:'SF Pro Display';
            font-size:14pt;
            font-weight:900;""")
        
        
            label2 = QLabel("HELLO! MY NAME IS EMRE")
            font = QFont('SF Pro Display', 14)
            font.setWeight(QFont.Weight.Black) # 900
            label2.setFont(font)
        
        
            lay.addWidget(label)
            lay.addWidget(label2)
            wid.show()
            sys.exit(app.exec())
        
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @Emrecp
        I don't know. But I would start by removing the font weight on each one. Are they then identical or different? And while you're there I presume it does not matter which font (name) you use (e.g. Arial)?

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

          Hi,

          Setting a stylesheet nukes the current style used to render the widget it's applied on hence you might see differences.

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

          JonBJ EmrecpE 2 Replies Last reply
          0
          • SGaistS SGaist

            Hi,

            Setting a stylesheet nukes the current style used to render the widget it's applied on hence you might see differences.

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

            @SGaist
            Either I misunderstand the OP or I misunderstand you or I misunderstand the code. He is comparing one example 100% stylesheet against another example 100% code, no? And wants/expects them to be identical.

            SGaistS 1 Reply Last reply
            1
            • JonBJ JonB

              @SGaist
              Either I misunderstand the OP or I misunderstand you or I misunderstand the code. He is comparing one example 100% stylesheet against another example 100% code, no? And wants/expects them to be identical.

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

              @JonB you understood correctly. But as I wrote, two different styles are used, hence there can be difference.

              I would also check the fonts effectively used by the widgets to compare their values.

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

              JonBJ 1 Reply Last reply
              0
              • SGaistS SGaist

                @JonB you understood correctly. But as I wrote, two different styles are used, hence there can be difference.

                I would also check the fonts effectively used by the widgets to compare their values.

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

                @SGaist said in Maintaining Consistent Font Appearance in PySide6 QLabel: QSS vs. QFont:

                But as I wrote, two different styles are used, hence there can be difference.

                I still don't understand. Maybe the OP will.

                ISTM he is asking whether

                    label.setStyleSheet("""font:'SF Pro Display';
                    font-size:14pt;
                    font-weight:900;""")
                

                should be equated identically via

                    font = QFont('SF Pro Display', 14)
                    font.setWeight(QFont.Weight.Black) # 900
                    label2.setFont(font)
                

                Setting a stylesheet nukes the current style used to render the widget

                Yes. But what current style was used for the QLabel in the above code? I don't see any? So I don't see setting the stylesheet has any style to nuke here?

                SGaistS 1 Reply Last reply
                1
                • JonBJ JonB

                  @SGaist said in Maintaining Consistent Font Appearance in PySide6 QLabel: QSS vs. QFont:

                  But as I wrote, two different styles are used, hence there can be difference.

                  I still don't understand. Maybe the OP will.

                  ISTM he is asking whether

                      label.setStyleSheet("""font:'SF Pro Display';
                      font-size:14pt;
                      font-weight:900;""")
                  

                  should be equated identically via

                      font = QFont('SF Pro Display', 14)
                      font.setWeight(QFont.Weight.Black) # 900
                      label2.setFont(font)
                  

                  Setting a stylesheet nukes the current style used to render the widget

                  Yes. But what current style was used for the QLabel in the above code? I don't see any? So I don't see setting the stylesheet has any style to nuke here?

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

                  @JonB each platform has a default style to render the application. Setting a stylesheet triggers the replacement of the current style by the one that handles the stylesheet hence the "strange" look you get when changing elements such as buttons or QTabBar. It's more visible on platforms such as macOS or Windows.

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

                  JonBJ 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    @JonB each platform has a default style to render the application. Setting a stylesheet triggers the replacement of the current style by the one that handles the stylesheet hence the "strange" look you get when changing elements such as buttons or QTabBar. It's more visible on platforms such as macOS or Windows.

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

                    @SGaist said in Maintaining Consistent Font Appearance in PySide6 QLabel: QSS vs. QFont:

                    each platform has a default style to render the application.

                    Ohhhh. Is this what I have seen about Fusion style and never understood? So that's some base CSS files for QLabels etc. to start with? I didn't know that, do you have the doc link which explains? Should be of interest to the OP.

                    SGaistS 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @SGaist said in Maintaining Consistent Font Appearance in PySide6 QLabel: QSS vs. QFont:

                      each platform has a default style to render the application.

                      Ohhhh. Is this what I have seen about Fusion style and never understood? So that's some base CSS files for QLabels etc. to start with? I didn't know that, do you have the doc link which explains? Should be of interest to the OP.

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

                      @JonB there's no CSS involved in the platform styles.

                      You can check the styles example.

                      KDAB has a very good article about dumping stylesheets in favor of custom styles.

                      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
                      • SGaistS SGaist

                        Hi,

                        Setting a stylesheet nukes the current style used to render the widget it's applied on hence you might see differences.

                        EmrecpE Offline
                        EmrecpE Offline
                        Emrecp
                        wrote on last edited by
                        #10

                        @SGaist I want to use this font in a paintEvent in my own project. Since I can't use stylesheet in paintEvent, I need how to get the same text with qfont. Just tell me the solution please

                        JonBJ 1 Reply Last reply
                        0
                        • EmrecpE Emrecp

                          @SGaist I want to use this font in a paintEvent in my own project. Since I can't use stylesheet in paintEvent, I need how to get the same text with qfont. Just tell me the solution please

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

                          @Emrecp
                          I believe @SGaist is telling you that you cannot guarantee that a widget with a stylesheet will look identical to one you construct with coded style attributes. As soon as you place a stylesheet on a widget you are (potentially) losing some styles which are inbuilt, so it may differ.

                          I suggested earlier you remove the weight stuff in your example and see whether just with the font in "normal" it does or does not look identical using the same font in both code and stylesheet.

                          If you are coding inside paintEvent() then, as you say, you cannot use stylesheet. You can only set the attributes on a QFont as you have done.

                          I don't know whether you can go through the sources of Qt to discover just what a current style like Fusion does.

                          1 Reply Last reply
                          2
                          • EmrecpE Emrecp has marked this topic as solved on

                          • Login

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