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. QListWidget.setStyleSheet
QtWS25 Last Chance

QListWidget.setStyleSheet

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 6 Posters 10.5k 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.
  • J Offline
    J Offline
    jschwartzman
    wrote on last edited by
    #1

    Each item in my ListWidget has different colored text on a white background. When selecting an item, I'd like to preserve the text color and indicate selection with a border or with a different color background. Everything I try sets the text color black for the selected item. Any suggestions?
    Thanks!

    JonBJ H 2 Replies Last reply
    0
    • J jschwartzman

      Each item in my ListWidget has different colored text on a white background. When selecting an item, I'd like to preserve the text color and indicate selection with a border or with a different color background. Everything I try sets the text color black for the selected item. Any suggestions?
      Thanks!

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

      @jschwartzman
      You should show whatever you tried? You need to get down to the QListWidgetItem level. And that is not a QWidget, so you cannot set stylesheet on it directly, only via QListWidget::item etc. See if https://stackoverflow.com/questions/32311927/qlistwidgetsetstylesheet-and-qlistwidgetitemsetbackgroundcolor-relation gives you an idea how others tackle this?

      1 Reply Last reply
      1
      • J Offline
        J Offline
        jschwartzman
        wrote on last edited by
        #3

        This is what I've tried:
        self.listWidgetRepo.setStyleSheet("""
        QlistWidget::item:selected { background: transparent; }
        QListWidget::item:selected { border: 1px solid red; }
        """)

        It paints the border, but renders the text foreground color black. If I change it to:
        self.listWidgetRepo.setStyleSheet("""
        QListWidget::item:selected { foreground: green;
        QListWidget::item:selected { background: transparent; }
        QListWidget::item:selected { border: 1px solid red; }
        """)

        The selected text foreground color is still black.

        Each item foreground color has a specific meaning and I'd like to preserve the foreground color while the item is selected.

        I looked at the example code you provided but couldn't understand how it related to the selected state of the QListWidget.
        Thanks.

        JonBJ J.HilkJ 3 Replies Last reply
        0
        • J jschwartzman

          This is what I've tried:
          self.listWidgetRepo.setStyleSheet("""
          QlistWidget::item:selected { background: transparent; }
          QListWidget::item:selected { border: 1px solid red; }
          """)

          It paints the border, but renders the text foreground color black. If I change it to:
          self.listWidgetRepo.setStyleSheet("""
          QListWidget::item:selected { foreground: green;
          QListWidget::item:selected { background: transparent; }
          QListWidget::item:selected { border: 1px solid red; }
          """)

          The selected text foreground color is still black.

          Each item foreground color has a specific meaning and I'd like to preserve the foreground color while the item is selected.

          I looked at the example code you provided but couldn't understand how it related to the selected state of the QListWidget.
          Thanks.

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

          @jschwartzman

          QListWidget::item:selected { foreground: green;

          You are missing the }.

          1 Reply Last reply
          1
          • J jschwartzman

            This is what I've tried:
            self.listWidgetRepo.setStyleSheet("""
            QlistWidget::item:selected { background: transparent; }
            QListWidget::item:selected { border: 1px solid red; }
            """)

            It paints the border, but renders the text foreground color black. If I change it to:
            self.listWidgetRepo.setStyleSheet("""
            QListWidget::item:selected { foreground: green;
            QListWidget::item:selected { background: transparent; }
            QListWidget::item:selected { border: 1px solid red; }
            """)

            The selected text foreground color is still black.

            Each item foreground color has a specific meaning and I'd like to preserve the foreground color while the item is selected.

            I looked at the example code you provided but couldn't understand how it related to the selected state of the QListWidget.
            Thanks.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @jschwartzman

            to add to @JonB

            QlistWidget::item:selected { background: transparent; }

            You wrote QlistWidget instead of QListWidget, makes a difference!


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            1
            • J jschwartzman

              This is what I've tried:
              self.listWidgetRepo.setStyleSheet("""
              QlistWidget::item:selected { background: transparent; }
              QListWidget::item:selected { border: 1px solid red; }
              """)

              It paints the border, but renders the text foreground color black. If I change it to:
              self.listWidgetRepo.setStyleSheet("""
              QListWidget::item:selected { foreground: green;
              QListWidget::item:selected { background: transparent; }
              QListWidget::item:selected { border: 1px solid red; }
              """)

              The selected text foreground color is still black.

              Each item foreground color has a specific meaning and I'd like to preserve the foreground color while the item is selected.

              I looked at the example code you provided but couldn't understand how it related to the selected state of the QListWidget.
              Thanks.

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

              @jschwartzman
              To add to posts from myself & @J-Hilk

              { foreground: ...

              Could you clarify where you got foreground from as an attribute/property to change text color? It's not on https://doc.qt.io/qt-5/stylesheet-reference.html. You didn't just type it in without verifying it was correct, then find it didn't work and were surprised, did you? There is a property named color for setting text color, as per HTML, have you tried that?

              And importantly, what about selection-color (& selection-background-color) which you see there:

              The foreground of selected text or items.
              This property is supported by all widgets that respect the QWidget::palette and that show selection text.

              Have you investigated that?

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jschwartzman
                wrote on last edited by
                #7

                This works, but it's not what a want (I changed from a QListWidget to a QListView, but I'm not sure I needed to):

                    self.listViewRepo.setStyleSheet("""
                    QListView::item:selected { color: blue; }
                    QListView::item:selected { background-color: white; }
                    QListView::item:selected { border: 1px solid red; }
                    """)
                

                This gives me a selected row with the text rendered in blue on a white background.
                What I want to know is how do I keep the same selected text foreground color that I set when I populated the list. I tried using self.listViewRepo.setProperty("textColor", color) when I populated the list, but I don't know how to access this property when the selection changes. And I don't know how to put it in the style sheet.
                Any suggestions?
                Thanks.

                J 1 Reply Last reply
                0
                • J jschwartzman

                  This works, but it's not what a want (I changed from a QListWidget to a QListView, but I'm not sure I needed to):

                      self.listViewRepo.setStyleSheet("""
                      QListView::item:selected { color: blue; }
                      QListView::item:selected { background-color: white; }
                      QListView::item:selected { border: 1px solid red; }
                      """)
                  

                  This gives me a selected row with the text rendered in blue on a white background.
                  What I want to know is how do I keep the same selected text foreground color that I set when I populated the list. I tried using self.listViewRepo.setProperty("textColor", color) when I populated the list, but I don't know how to access this property when the selection changes. And I don't know how to put it in the style sheet.
                  Any suggestions?
                  Thanks.

                  J Offline
                  J Offline
                  jschwartzman
                  wrote on last edited by
                  #8

                  @jschwartzman In other words, I want the selected text foreground color in my QListView to be a variable. It doesn't seem that I can do that with setStyleSheet. Can anyone point me in the right direction?
                  Thanks.

                  JonBJ O 2 Replies Last reply
                  0
                  • J jschwartzman

                    @jschwartzman In other words, I want the selected text foreground color in my QListView to be a variable. It doesn't seem that I can do that with setStyleSheet. Can anyone point me in the right direction?
                    Thanks.

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

                    @jschwartzman

                    • When the selection changes you get a signal. For, say, a QListView you need to look at self.listViewRepo.selectionModel().currentChanged signal. This means using https://doc.qt.io/qt-5/qitemselectionmodel.html.

                    • Assuming the setProperty("textColor", color) works (I don't know if it does), then you just have to specify the correct desired color and you have it working as a variable.

                    • If you want to do it via a stylesheet, in the string passed to self.listViewRepo.setStyleSheet you need to alter the QListView::item:selected { color: blue; } fragment in the string to output the desired color in-line, something like:

                    """
                    ...
                    QListView::item:selected { color: """ + color + """; }
                    ...
                    """
                    
                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      jschwartzman
                      wrote on last edited by
                      #10

                      That did it. Many thanks!

                      1 Reply Last reply
                      0
                      • J jschwartzman

                        @jschwartzman In other words, I want the selected text foreground color in my QListView to be a variable. It doesn't seem that I can do that with setStyleSheet. Can anyone point me in the right direction?
                        Thanks.

                        O Offline
                        O Offline
                        oncewealthy
                        wrote on last edited by
                        #11

                        @jschwartzmandordle I'm having trouble linking; what statement do I need to use? As far as I know, LIBS is what's used to create dlls and other types of library files.

                        JonBJ 1 Reply Last reply
                        0
                        • O oncewealthy

                          @jschwartzmandordle I'm having trouble linking; what statement do I need to use? As far as I know, LIBS is what's used to create dlls and other types of library files.

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

                          @oncewealthy
                          Hello and welcome.

                          I don't know what your issue is, but for sure it has nothing to do with the setStyleSheet() that this topic is about. Please open your own new topic if you have questions about linking.

                          1 Reply Last reply
                          0
                          • J jschwartzman

                            Each item in my ListWidget has different colored text on a white background. When selecting an item, I'd like to preserve the text color and indicate selection with a border or with a different color background. Everything I try sets the text color black for the selected item. Any suggestions?
                            Thanks!

                            H Offline
                            H Offline
                            HipolitoHickman
                            wrote on last edited by
                            #13
                            This post is deleted!
                            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