QComboBox color change
-
Gi
Im not sure what you are trying.
"I'm trying to change the colour of a QComboBox"
Of the items or what do you mean? If you change color of whole combox it would
be all of the items. so what do you try ?Something like this
http://doc.qt.io/qt-4.8/qt-itemviews-coloreditorfactory-example.html -
Hi ,
I' already changed the color of the items I put in my combobox the thing is when the list is not opened , I cannot see the item selected because it won't change color.
As you can see on the pictures I gave , my items are blue but when selected the item displayed is in grey or dark grey. I'd like it blue so we can see it.Thanks.
-
Hi,
here are some stylesheet examples for a combobox: http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qcombobox. I am not sure, though, that you find what you search for.
-Michael. -
After a check it appears that I don't really understand how to use the code.
I assume it's something likeui->comboBox->setStyleSheet(QComboBox::/*don't know what to put here */)
If you could explain that to me it would be great .
Thanks in advance .
-Amaury -
Its stylesheet syntax so its the same as before
QComboBox:!editable, QComboBox::drop-down:editable { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #E1E1E1, stop: 0.4 #DDDDDD, stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3); }
is just a string.
"QComboBox:!editable, QComboBox::drop-down:editable {background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);}";
This becomes really ugly to have in code so often loading it from a file is better.
BUt its 100% the same as
setStyleSheet("background-color: rgb(0, 0, 0) ; color: rgb(0, 152, 152);"
just with more stylesheet code/tags. -
Ok so if thse stylesheet don't change the text color in my combobox how can I do ? ^^
I already tried to change the palette colors and the ui file with text editor but none of that change anything.
The only way i found is to set the combo box as editable and then the text will appear in blue but I don't wan't that someone can edit the combobox.
If there's any way to di it well ...
-
Stylesheets are just strings.
Nothing change in them unless u change the string and reapply with setStyleSheet.
I think you are after
QComboBox:editable {}
or if only when "open"
Docs says
"the pop-up of the QComboBox is a QAbstractItemView and is styled using the descendant selector:"
so maybe what u are after isQComboBoxQAbstractItemView {}
When stylesheets do nothing, it often syntax error or simply not the right selector.
-
I'm going to check the QAbstractItemView, maybe that's what I'm looking for you're right.
The thing I really don't understand is that my whole text and backgound is correctly set up when I open my combo box but when it's not there's a completly diffenrent setup (I can change the text font but not the color ) that's absurd ....
Coming back to you after a quick research.
-
@Amaury
-The thing I really don't understand is that my whole text and backgound is correctly set up when I open my combo boxWell if you use a selector like QComboBox it will not affect other types than QComboBox so it wont affect
the QAbstractItemView.
If you use QWidget as selector it affects all( from same parent)
but also buttons and stuff so its best to be exact.Think of it as u say:
Color all boxes.
And the thing combobox uses when open, is a circle so wont color it.So using selectors correctly makes it work much better :)
selector can be a name, a (class) type and sub item.http://doc.qt.io/qt-5/stylesheet-reference.html
This is important to understand to use stylesheets. -
-
@Amaury
A combox is multiple widgets.
There is the combox it selv, an edit control sometimes and
the dropdown area is a QAbstractItemView
and so on.
This goes for MOST widgets.
http://doc.qt.io/qt-5/stylesheet-reference.html
Look at all the differnt types for each Class.So yes, you cannot not just color the combo box and expect to work for all sub objects and types.
However, using QWidget as selector will affect it all. But its too broad.
So yes., its more complex than just think "i color the combobox" when you mean the
view's items. Its not the same. :) -
@Amaury !
Yes, test it out. once learned it make more sense.
For composite widgets like the combo box, its a bit more involving than for QPushButton.
Also know its cascading.
This is from setting "QWidget { background-color: rgb(170, 0, 127) ;}"
on the mainwindow.. -
Hi there I'm coming back to you and have to thank you I just understand how the stylesheets works now , I mean I was using the automatic parameters and was never specifiying any items like QComboBox or QPushButton.
I think I was stuck on that too long to notice what I was missing thank you a lot .