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 to change the text color of a QComboBox using Style Sheets

How to change the text color of a QComboBox using Style Sheets

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 6.3k Views 1 Watching
  • 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.
  • L Offline
    L Offline
    laseranichris
    wrote on last edited by
    #1

    I want to change the text color of a QComboBox depending on the selection done by the user:
    If the selected item is not valid at this time it shall be displayed in red.

    I did this using the following code:

    @
    QPalette palette = combo->palette();
    palette.setColor(QPalette::Text, condition ? Qt::black : Qt::red);
    combo->setPalette(palette);
    @

    This worked but my application uses style sheets and the base style "Cleanlook" and as soon as a style change event occurs the color is reset to it's default (black).

    When I set the color of the QComboBox within the style sheet not only the color of the text but also the color of the arrow and the popup list is changed.

    @
    MyCombo
    {
    color: red;
    }
    @

    Is there a way to change only the text color using style sheets?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      You can have a look at "Trigger an update for the widget while using dynamic properties":https://qt-project.org/doc/qt-5.0/qtwidgets/stylesheet-syntax.html#note-238 and "QLineEdit - Indicate if user enters an Invalid Value":https://qt-project.org/forums/viewthread/25149/ thread.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        laseranichris
        wrote on last edited by
        #3

        Thank you for your answer.

        I know how to change the style dynamically.

        My problem is: I cannot get access to the Combo Box button to change the color.
        I can get access for the drop down list using QListView:

        @
        MyCombo QListView
        {
        color: red;
        }
        @

        I already tried QLineEdit, QPushButton, QLabel...

        But nothing worked.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          It will be helpful if you can attach an image of the output that you are getting.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            laseranichris
            wrote on last edited by
            #5

            I attached a screenshot of how it should look like:

            !http://dl.dropbox.com/u/62032812/Combo.jpg()!

            The problem is: Whatever I try, the text color stays black.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              laseranichris
              wrote on last edited by
              #6

              Sorry, there was a problem with the link.
              Here is the right one:

              http://dl.dropbox.com/u/62032812/Combo.jpg

              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