Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved]Set QCombobox:selected color to match Linux desktop "Theme".
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CraigH
    wrote on last edited by
    #1

    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}");
    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CraigH
        wrote on last edited by
        #3

        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);}")
        .arg(A.palette().highlight().color().red())
        .arg(A.palette().highlight().color().green())
        .arg(A.palette().highlight().color().blue()));
        @

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            CraigH
            wrote on last edited by
            #5

            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;}")
            .arg(A.palette().highlight().color().name()));
            @

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved