How to change QComboBox color without change the apparence
-
Hi ! I would like to turn a combobox in white.
So I tried to use StyleSheet with background-color and the QPalette way.
It works, my combobox turns white but its apparence looks like Windows XP Combobox...My goal is to change the color of my combobox's background while keeping its Windows 10 appearance.
Before :
After:
Thanks for helping !
-
This topic is close to my case :
https://stackoverflow.com/questions/54160285/how-to-set-background-color-of-qcombobox-button -
@Loann
And it seems to me the accepted solution there is likely the case. If you alter any property you are in danger of losing the whole "theme".Note: With complex widgets such as QComboBox and QScrollBar, if one property or sub-control is customized, all the other properties or sub-controls must be customized as well.
It looks like you have to customize everything if you want to customize the button... Then it would not be possible to change the colour without overwriting or suppressing the standard style.
-
Hello!
Yes, you must change a few properties of
QComboBox
to make it look nice. Here is my code using only theCSS
stylesheet:ui->comboBox->setStyleSheet("QComboBox {background-color: #FFFFFF; border: 1px solid lightgray;} QComboBox::drop-down {border: none;} " "QComboBox::down-arrow {image: url(:/Icons/arrow.png);} " "QComboBox QAbstractItemView {background-color: #FFFFFF; selection-background-color: #FFFFFF; selection-color: #000000;}");
I have used a custom arrow icon similar to the default one and attached it to the program resources:
Result:
If you do not want the selection color, so feel free to remove this line of code:
"QComboBox QAbstractItemView {background-color: #FFFFFF; selection-background-color: #FFFFFF; selection-color: #000000;}"
, then you will get the default selection color. Also, I would suggest you to apply your stylesheet from a resource file at program startup for better performance. You can get more details about Qt style sheets in the docs: https://doc.qt.io/qt-5/stylesheet-examples.html
Happy coding!