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. setStyleSheet doesn't change font on QListWidget, but changes background color
QtWS25 Last Chance

setStyleSheet doesn't change font on QListWidget, but changes background color

Scheduled Pinned Locked Moved Unsolved Qt for Python
9 Posts 4 Posters 2.4k 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.
  • M Offline
    M Offline
    midnightdim
    wrote on last edited by midnightdim
    #1

    I have a QListWidget, some items have custom background colors set via setBackground().
    I'm trying to change the font weight of selected items in QListWidget with the following code:
    self.setStyleSheet("QListWidget::item:selected {color: red; border: 2px solid black; font: bold;}")

    It does make the text color of the selected items red and it does add the border, but it doesn't make the font bold (I also tried italic and also tried font-weight:bold; - nothing worked). Why is that?

    Also, it changes the item background although I don't need that, I need to keep the background of the items.
    My goal is to indicate the selected items visually because the default styling doesn't work well with my custom backgrounds.

    A related question is: if I set an image in the stylesheet with image-position: left; style it overlaps the text. Is there a way to set some fixed size indentation for text? There's a workaround - I can put the image to the right, but would be cool to have it on the left.

    I'm using PySide6.

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

      Hi,

      It looks like you should rather implement a custom QStyledItemDelegate.

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

      M 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        It looks like you should rather implement a custom QStyledItemDelegate.

        M Offline
        M Offline
        midnightdim
        wrote on last edited by
        #3

        @SGaist Thanks for the idea. But my question is: why can't I use the stylesheet approach? It seems to work, but it works in a strange way - some styles are applied, some are ignored, some override the default styles. I would like to understand why it works like that. Maybe I can achieve what I need with the stylesheets - after all they are intended specifically for this if I understand it correctly.

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

          Sorry, I wasn't precise enough. When you start using stylesheet you have to do the complete styling as the application default style is not used anymore and is replaced by the one handling the stylesheet.

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

          M 1 Reply Last reply
          1
          • SGaistS SGaist

            Sorry, I wasn't precise enough. When you start using stylesheet you have to do the complete styling as the application default style is not used anymore and is replaced by the one handling the stylesheet.

            M Offline
            M Offline
            midnightdim
            wrote on last edited by
            #5

            @SGaist Thanks. What about setStyleSheet changing the color, but not changing the font weight? Is this a bug? Or is this by design?

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

              Can you provide a minimal runnable script so we can reproduce this locally ?

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

              M 1 Reply Last reply
              0
              • SGaistS SGaist

                Can you provide a minimal runnable script so we can reproduce this locally ?

                M Offline
                M Offline
                midnightdim
                wrote on last edited by
                #7

                @SGaist Sure, here it is:

                from PySide2.QtWidgets import QListWidget, QApplication
                
                app = QApplication()
                
                listw = QListWidget()
                listw.addItems(['lorem','ipsum'])
                listw.setStyleSheet("QListWidget::item:selected {color: red; border: 2px solid black; font: bold;}")
                listw.show()
                
                app.exec_()
                

                If you select a list item, the color changes to red and the border appears, but the font weight doesn't change (I tried bold and italic).

                JonBJ 1 Reply Last reply
                0
                • M midnightdim

                  @SGaist Sure, here it is:

                  from PySide2.QtWidgets import QListWidget, QApplication
                  
                  app = QApplication()
                  
                  listw = QListWidget()
                  listw.addItems(['lorem','ipsum'])
                  listw.setStyleSheet("QListWidget::item:selected {color: red; border: 2px solid black; font: bold;}")
                  listw.show()
                  
                  app.exec_()
                  

                  If you select a list item, the color changes to red and the border appears, but the font weight doesn't change (I tried bold and italic).

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

                  @midnightdim
                  This has always been the case, e.g. see QListView item font StyleSheet not working:

                  seems like setting the font via stylesheet for the ::itemsubcotnrol isn't supported at all. Only setting the font on the item-widget itself, but then the font gets applied for all items.

                  That post suggests the only way to achieve it is via data(Qt::FontRole) method.

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    EthanFinn
                    wrote on last edited by
                    #9

                    @fnaf world You can try using the font-weight property in your style sheet to make the font bold for the selected items in the QListWidget. It should be set as follows: "font-weight: bold;". As for the background color issue, you can specify the background color for the selected items separately using "background-color: <your_color>;" in the style sheet.

                    1 Reply Last reply
                    0

                    • Login

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