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. Change QComboBox popup highlighted item background color
QtWS25 Last Chance

Change QComboBox popup highlighted item background color

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 11.1k 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.
  • Q Offline
    Q Offline
    qt.1234
    wrote on 11 Nov 2019, 10:20 last edited by
    #1

    Hi,

    I would like to change the background color of the selected item in my QCombobox's drop down list. However, I am not sure how to go about doing it. Please advice. Thank you.

    J 1 Reply Last reply 11 Nov 2019, 11:40
    0
    • Q qt.1234
      11 Nov 2019, 10:20

      Hi,

      I would like to change the background color of the selected item in my QCombobox's drop down list. However, I am not sure how to go about doing it. Please advice. Thank you.

      J Offline
      J Offline
      JonB
      wrote on 11 Nov 2019, 11:40 last edited by
      #2

      @qt-1234
      Start out from:
      https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qcombobox
      https://stackoverflow.com/questions/43347419/how-to-change-the-color-of-non-selected-item-in-qcombobox-style-sheet

      item:selected& selection-background-color are of interest.

      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        qt.1234
        wrote on 12 Nov 2019, 03:30 last edited by
        #3

        Hi! Setting stylesheet for the QComboBox doesnt work. Shown below is one of the recommendation shown in one of the link you have shared.

        QComboBox QAbstractItemView {
        
            selection-background-color: rgb(255, 0, 0);
            selection-color: rgb(200, 200, 200);
        }
        

        how do i go about using this? Do i use it as a stylesheet? or using setView? Selection-background-color refers to the highlighted color while selection-color refers to the font?

        please advice thank you!

        Q 1 Reply Last reply 12 Nov 2019, 08:10
        1
        • Q qt.1234
          12 Nov 2019, 03:30

          Hi! Setting stylesheet for the QComboBox doesnt work. Shown below is one of the recommendation shown in one of the link you have shared.

          QComboBox QAbstractItemView {
          
              selection-background-color: rgb(255, 0, 0);
              selection-color: rgb(200, 200, 200);
          }
          

          how do i go about using this? Do i use it as a stylesheet? or using setView? Selection-background-color refers to the highlighted color while selection-color refers to the font?

          please advice thank you!

          Q Offline
          Q Offline
          qwasder85
          wrote on 12 Nov 2019, 08:10 last edited by qwasder85
          #4

          @qt-1234

          edit: Sorry for the typo, of course it's QComboBox instead of ComboBox.

          Do this in your code:

          my_combobox->setStylesheet("QComboBox { selection-background-color: red; }");
          

          Obviously, change the "red" to whatever you desire. Also, have a look at this to get more inspiration on how to customize a QComboBox with stylesheets. It's very powerful, but the documentation is horrible for this stuff: https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qcombobox

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qt.1234
            wrote on 12 Nov 2019, 10:26 last edited by
            #5

            i have tried, but the highlighted item still remain blue. :(

            J 1 Reply Last reply 12 Nov 2019, 11:05
            0
            • Q qt.1234
              12 Nov 2019, 10:26

              i have tried, but the highlighted item still remain blue. :(

              J Offline
              J Offline
              JonB
              wrote on 12 Nov 2019, 11:05 last edited by
              #6

              @qt-1234
              Not sure what you tried. If it was what @qwasder85 typed in above, ComboBox will be wrong, you'll need to try with QComboBox ....

              I don't know exactly what you will want, you'll have to play and read. But I would start by verifying that from the docs page you can indeed get some of those samples to work, soi you know you're doing stylesheet right, and then move on to exactly what you need.

              Q 1 Reply Last reply 14 Nov 2019, 15:55
              0
              • Q Offline
                Q Offline
                qt.1234
                wrote on 14 Nov 2019, 02:47 last edited by qt.1234
                #7

                What i have tried is setting my combobox's stylesheet.

                ComboBox->setStylesheet("QComboBox { selection-background-color: rgb(0,255,0); }");

                or i have to use item delegate in this case?

                J 1 Reply Last reply 14 Nov 2019, 14:23
                0
                • Q qt.1234
                  14 Nov 2019, 02:47

                  What i have tried is setting my combobox's stylesheet.

                  ComboBox->setStylesheet("QComboBox { selection-background-color: rgb(0,255,0); }");

                  or i have to use item delegate in this case?

                  J Offline
                  J Offline
                  JonB
                  wrote on 14 Nov 2019, 14:23 last edited by JonB
                  #8

                  @qt-1234
                  It may not make any difference, but if you are setting this directly on a QComboBox instance (I thought you mean for all QComboBoxes), you only need:

                  ComboBox->setStylesheet("selection-background-color: rgb(0,255,0)");
                  

                  However, there is some doubt over how this may have changed in various Qt versions. Also try instead:

                  ComboBox->setStylesheet("QComboBox::item:selected { background-color: rgb(0,255,0); }");
                  

                  There are also suggestions in the links I quoted earlier which mention that you or may not need to put the attribute on QComboBox QAbstractItemView....

                  One of these really should work!

                  Q 1 Reply Last reply 15 Nov 2019, 09:08
                  0
                  • J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 14 Nov 2019, 14:26 last edited by
                    #9

                    Just to add my 2 cents to this conversation, I recently found this bugreport:
                    https://bugreports.qt.io/browse/QTBUG-77006

                    That may or may not play a role here. I would suggest updating to 5.12.6 or 5.13.2, just in case 😉


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    0
                    • J JonB
                      12 Nov 2019, 11:05

                      @qt-1234
                      Not sure what you tried. If it was what @qwasder85 typed in above, ComboBox will be wrong, you'll need to try with QComboBox ....

                      I don't know exactly what you will want, you'll have to play and read. But I would start by verifying that from the docs page you can indeed get some of those samples to work, soi you know you're doing stylesheet right, and then move on to exactly what you need.

                      Q Offline
                      Q Offline
                      qwasder85
                      wrote on 14 Nov 2019, 15:55 last edited by
                      #10

                      @JonB Sorry, of course QComboBox...

                      1 Reply Last reply
                      0
                      • J JonB
                        14 Nov 2019, 14:23

                        @qt-1234
                        It may not make any difference, but if you are setting this directly on a QComboBox instance (I thought you mean for all QComboBoxes), you only need:

                        ComboBox->setStylesheet("selection-background-color: rgb(0,255,0)");
                        

                        However, there is some doubt over how this may have changed in various Qt versions. Also try instead:

                        ComboBox->setStylesheet("QComboBox::item:selected { background-color: rgb(0,255,0); }");
                        

                        There are also suggestions in the links I quoted earlier which mention that you or may not need to put the attribute on QComboBox QAbstractItemView....

                        One of these really should work!

                        Q Offline
                        Q Offline
                        qt.1234
                        wrote on 15 Nov 2019, 09:08 last edited by
                        #11

                        @JonB i realize it will only work if the highlight is invoked using a mouse event. Whereas in by case, I am trying to change the highlight color when a signal is invoked.

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          qt.1234
                          wrote on 19 Nov 2019, 04:07 last edited by
                          #12

                          I have a solution to this.

                          Linking signal currentIndexChanged() to a slot. So whenever there is a change of state in the index, the slot will be invoked. And in the slot do the following:

                          void MainWindow::indexChangeSlot()
                          {
                          QPalette p = comboBox->palette();
                          p.setColor(QPalette::Highlight, QColor(0,255,0));
                          comboBox->setPalette(p);
                          
                          p = comboBox->view()->palette();
                          p.setColor(QPalette::Highlight, QColor(0,255,0));
                          
                          comboBox->view()->setPalette(p);
                          }
                          
                          Pablo J. RoginaP 1 Reply Last reply 19 Nov 2019, 12:58
                          2
                          • Q qt.1234
                            19 Nov 2019, 04:07

                            I have a solution to this.

                            Linking signal currentIndexChanged() to a slot. So whenever there is a change of state in the index, the slot will be invoked. And in the slot do the following:

                            void MainWindow::indexChangeSlot()
                            {
                            QPalette p = comboBox->palette();
                            p.setColor(QPalette::Highlight, QColor(0,255,0));
                            comboBox->setPalette(p);
                            
                            p = comboBox->view()->palette();
                            p.setColor(QPalette::Highlight, QColor(0,255,0));
                            
                            comboBox->view()->setPalette(p);
                            }
                            
                            Pablo J. RoginaP Offline
                            Pablo J. RoginaP Offline
                            Pablo J. Rogina
                            wrote on 19 Nov 2019, 12:58 last edited by
                            #13

                            @qt-1234 said in Change QComboBox popup highlighted item background color:

                            I have a solution to this.

                            Great, so time to call your post as solved? Please don't forget to mark it as such. Thanks

                            Upvote the answer(s) that helped you solve the issue
                            Use "Topic Tools" button to mark your post as Solved
                            Add screenshots via postimage.org
                            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                            1 Reply Last reply
                            1

                            2/13

                            11 Nov 2019, 11:40

                            11 unread
                            • Login

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