How to change the text color of a QComboBox using Style Sheets
-
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?
-
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.
-
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.
-
It will be helpful if you can attach an image of the output that you are getting.
-
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.
-
Sorry, there was a problem with the link.
Here is the right one: