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.
  • E Offline
    E Offline
    Emrecp
    wrote on 10 Aug 2023, 19:05 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())
    
    
    J 1 Reply Last reply 10 Aug 2023, 19:15
    0
    • S SGaist moved this topic from General and Desktop on 10 Aug 2023, 19:11
    • E Emrecp
      11 Aug 2023, 06:36

      @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

      J Offline
      J Offline
      JonB
      wrote on 11 Aug 2023, 06:50 last edited by JonB 8 Nov 2023, 17:31
      #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
      • E Emrecp
        10 Aug 2023, 19:05

        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())
        
        
        J Offline
        J Offline
        JonB
        wrote on 10 Aug 2023, 19:15 last edited by JonB 8 Oct 2023, 19:17
        #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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 10 Aug 2023, 19:17 last edited by SGaist 8 Oct 2023, 19:22
          #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

          J E 2 Replies Last reply 10 Aug 2023, 19:19
          0
          • S SGaist
            10 Aug 2023, 19:17

            Hi,

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

            J Offline
            J Offline
            JonB
            wrote on 10 Aug 2023, 19:19 last edited by JonB 8 Oct 2023, 19:19
            #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.

            S 1 Reply Last reply 10 Aug 2023, 19:22
            1
            • J JonB
              10 Aug 2023, 19:19

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

              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 10 Aug 2023, 19:22 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

              J 1 Reply Last reply 10 Aug 2023, 19:27
              0
              • S SGaist
                10 Aug 2023, 19:22

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

                J Offline
                J Offline
                JonB
                wrote on 10 Aug 2023, 19:27 last edited by JonB 8 Oct 2023, 19:29
                #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?

                S 1 Reply Last reply 10 Aug 2023, 19:35
                1
                • J JonB
                  10 Aug 2023, 19:27

                  @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?

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 10 Aug 2023, 19:35 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

                  J 1 Reply Last reply 10 Aug 2023, 19:37
                  1
                  • S SGaist
                    10 Aug 2023, 19:35

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

                    J Offline
                    J Offline
                    JonB
                    wrote on 10 Aug 2023, 19:37 last edited by JonB 8 Oct 2023, 19:39
                    #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.

                    S 1 Reply Last reply 10 Aug 2023, 19:41
                    0
                    • J JonB
                      10 Aug 2023, 19:37

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

                      S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 10 Aug 2023, 19:41 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
                      • S SGaist
                        10 Aug 2023, 19:17

                        Hi,

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

                        E Offline
                        E Offline
                        Emrecp
                        wrote on 11 Aug 2023, 06:36 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

                        J 1 Reply Last reply 11 Aug 2023, 06:50
                        0
                        • E Emrecp
                          11 Aug 2023, 06:36

                          @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

                          J Offline
                          J Offline
                          JonB
                          wrote on 11 Aug 2023, 06:50 last edited by JonB 8 Nov 2023, 17:31
                          #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
                          • E Emrecp has marked this topic as solved on 11 Aug 2023, 17:24

                          4/11

                          10 Aug 2023, 19:19

                          topic:navigator.unread, 7
                          • Login

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