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. How to set QComboBox text color using setStyleSheet?
Qt 6.11 is out! See what's new in the release blog

How to set QComboBox text color using setStyleSheet?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 11.2k Views
  • 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.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by TomNow99
    #1

    Hello,

    I would like to change text color in QComboBox like this:

    ddd.png

    eeee.png

    ( I only want red text "Text abcd…." like in the picture). I know that I can use QLineEdit, but I would like to use setStyleSheet

    jsulmJ 1 Reply Last reply
    0
    • T TomNow99

      Hello,

      I would like to change text color in QComboBox like this:

      ddd.png

      eeee.png

      ( I only want red text "Text abcd…." like in the picture). I know that I can use QLineEdit, but I would like to use setStyleSheet

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @TomNow99 https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qcombobox

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • T Offline
        T Offline
        TomNow99
        wrote on last edited by
        #3

        @jsulm Thank you, but I saw this document. I don't see any example to QComboBox which I can use. But of course I can be wrong.

        JonBJ 1 Reply Last reply
        0
        • T TomNow99

          @jsulm Thank you, but I saw this document. I don't see any example to QComboBox which I can use. But of course I can be wrong.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @TomNow99
          The whole of that link is an example of customizing QComboBox.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            TomNow99
            wrote on last edited by
            #5

            @JonB I try:

            ui->comboBox->setStyleSheet("QLineEdit {color: red}");
            ui->comboBox->setStyleSheet("QListView::item:selected {background:red}");
            

            and many other but with no good result.

            JonBJ 1 Reply Last reply
            0
            • T TomNow99

              @JonB I try:

              ui->comboBox->setStyleSheet("QLineEdit {color: red}");
              ui->comboBox->setStyleSheet("QListView::item:selected {background:red}");
              

              and many other but with no good result.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @TomNow99 said in How to set QComboBox text color using setStyleSheet?:

              ui->comboBox->setStyleSheet("QListView::item:selected {background:red}");

              Why do you use QListView here? The link states:

              The pop-up of the QComboBox is a QAbstractItemView and is styled using the descendant selector:

              QComboBox QAbstractItemView {
                  border: 2px solid darkgray;
                  selection-background-color: lightgray;
              }
              

              so why not read that and try that example?

              1 Reply Last reply
              1
              • T Offline
                T Offline
                TomNow99
                wrote on last edited by
                #7

                @JonB I try this:

                QComboBox QAbstractItemView {
                    border: 2px solid darkgray;
                    selection-background-color: lightgray;
                }
                

                But this is not what I want. I don't want change items colors but only header color.

                JonBJ 1 Reply Last reply
                0
                • T TomNow99

                  @JonB I try this:

                  QComboBox QAbstractItemView {
                      border: 2px solid darkgray;
                      selection-background-color: lightgray;
                  }
                  

                  But this is not what I want. I don't want change items colors but only header color.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @TomNow99 said in How to set QComboBox text color using setStyleSheet?:

                  I don't want change items colors but only header color.

                  How does your proposed ui->comboBox->setStyleSheet("QListView::item:selected {background:red}"); address that?

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    TomNow99
                    wrote on last edited by
                    #9

                    @JonB I try many options. I would like that what I write in first post and add 2 example images. I only want change header text in QComboBox.

                    JonBJ 1 Reply Last reply
                    0
                    • T TomNow99

                      @JonB I try many options. I would like that what I write in first post and add 2 example images. I only want change header text in QComboBox.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @TomNow99
                      One way of achieving that with stylesheet is:

                      ui->comboBox->lineEdit()->setStyleSheet("color: red;");
                      
                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        TomNow99
                        wrote on last edited by TomNow99
                        #11

                        @JonB When I do this my app crash because lineEdit() return QWidget(0x0).

                        I read that only editable QComboboxes have lineEdites()

                        JonBJ 1 Reply Last reply
                        0
                        • T TomNow99

                          @JonB When I do this my app crash because lineEdit() return QWidget(0x0).

                          I read that only editable QComboboxes have lineEdites()

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          @TomNow99
                          That will happen if you have not done ui->comboBox->setEditable(true), because it won't have a line edit. But you must have done that, because you want your combobox to be editable so that it has a line to type into, mustn't you....? Please take the time to read https://doc.qt.io/qt-5/qcombobox.html#lineEdit if you want to start using lineEdit().

                          I find you can also do

                          ui->comboBox->lineEdit().setObjectName("combobox");
                          
                          Stylesheet:
                          #combobox {color: green;}
                          

                          if you prefer the ability to do it that way. I think you cannot access the lineEdit() from QSS just via QComboBox QLineEdit { ... }, for whatever reason.

                          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