Important: Please read the Qt Code of Conduct -

[solved]Set QCombobox:selected color to match Linux desktop "Theme".

  • In order to make the qcombobox which currently has focus more identifiable (especially on small screens), I want to change the sytle for the "selected" state. The code below does that, but specifies a hard coded color. If I leave out the color style, the border is "black", not the normal "highlight". How do I tell Qt to use the normal "highlight" color which it retrieves from the desktop theme.

    @QApplication A(argc, argv);
    A.setStyleSheet("QComboBox:selected{ border-width:2px; border-style:solid; border-color:blue}");

  • Lifetime Qt Champion


    You can retrieve the color using the palette of the application and put it in your style sheet string

  • Thanks for the help. I always thought of the palette and style sheet as being completely separate, and would never have thought of your method. Here is the code I ended up using.

    @QApplication A(argc, argv);
    A.setStyleSheet(QString("QComboBox:selected{ border-width:2px;"
    "border-style:solid; border-color:rgb(%1, %2, %3);}")

  • Lifetime Qt Champion

    You're welcome !

    IIRC, you can even simplify the code using the string notation #XXXXXX of the color (what QColor does when converted to a QString).

    If this solves you're problem, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

  • Found the function you were talking about. It makes for much cleaner code. Thanks again.

    @QApplication A(argc, argv);
    A.setStyleSheet(QString("QComboBox:selected { border-width:2px; "
    "border-style:solid; border-color:%1;}")

Log in to reply