Important: Please read the Qt Code of Conduct -

QComboBox color change

  • Hi there ,

    I'm trying to change the colour of a QComboBox . Until here nothing very difficult , I changed the colour as green blue and the background colour as black.
    All of that works except on the static state of the combo box .

    I mean when I'm clicking on the arrow I have the good colours but when I click on a value the colours are not set anymore .

  • Hi! Please show some code, otherwise no-one can help you ;-)

  • I don't really have code for that i'm changing the properties in QT Creator.

    The only thing I tried was to use the Qt properties like

    ui->comboBox->setStyleSheet("background-color: rgb(0, 0, 0) ; color: rgb(0, 152, 152);");

    That doesn't help, i've tried to put that on the index change signal /slot but didn't work.

    //this is my slot
    void Alarmes:: ComboBox_Value_Changed(int index)
        if (index == 0)
          my code here 
          ui->comboBox->setStyleSheet("background-color: rgb(0, 0, 0) ; color: rgb(0, 152, 152);");
        else if (index == 1)
           my code here 
        else if (index == 2)
           my code here 
        else if (index == 3)
            my code here 

  • up , I really need some help on this I've been looking around for a solution and did not found what I need :/

    thanks in advance.

  • Lifetime Qt Champion

    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

  • 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.


  • Hi,
    here are some stylesheet examples for a combobox: I am not sure, though, that you find what you search for.

  • Thank you i'm going to check this and coming back to you if nothing is solved :)

  • After a check it appears that I don't really understand how to use the code.
    I assume it's something like

    ui->comboBox->setStyleSheet(QComboBox::/*don't know what to put here */)

    If you could explain that to me it would be great .

    Thanks in advance .

  • Lifetime Qt Champion

    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 ...

  • Lifetime Qt Champion

    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 is

    QComboBoxQAbstractItemView {}

    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.

  • Lifetime Qt Champion

    -The thing I really don't understand is that my whole text and backgound is correctly set up when I open my combo box

    Well 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.
    This is important to understand to use stylesheets.

  • Correct me if I'm wrong, but what you're trying to tell me is that the content of the combo box is not the same object that the Combobox button and that I don't change the good properties though?

    Sorry I did not understand everything I am maybe wrong .

  • Lifetime Qt Champion

    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.
    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. :)

  • Alright i'm going to work on it, I wasn't thought that was that hard ^^ I used mostly buttons and things like that which were easier ;)

  • Lifetime Qt Champion

    @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.
    alt text

    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 .

  • Lifetime Qt Champion

    I assume you did notice the build in style sheet editor
    if you right click any Widget in designer?
    ( and choose change style sheet)

  • I'm using Qt Creator and not designer so I don't have the change style sheet option on right cliking a widget , but I can edit the style sheet by using the properties editor.

Log in to reply