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 background color in QComboBox?

How to set background color in QComboBox?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 2.7k 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 something like that:
    When I add next item to QComboBox, I would like to set its background color ( don't change that later ). Example:

    example.png

    EDIT
    And I would like one more thing:
    When I choose "two" I would like that the choosen background item will be in the same color ( red ). In this example the choosen item text is "one" ( I think about that "one" above beacuse there are 2 x "one" ).

    e2.png

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

      Hi
      you can do it via the model

              const QModelIndex idx = colorComboBox->model()->index(x, y);
              colorComboBox->model()->setData(idx, Qt:red, Qt::BackgroundColorRole);
              colorComboBox->model()->setData(idx, Qt::blue, Qt::ForegroundRole);
      
      
      1 Reply Last reply
      1
      • T Offline
        T Offline
        TomNow99
        wrote on last edited by TomNow99
        #3

        @mrjj Thank you :)
        This is exactly what I need.

        But I Edit my post, so I need one more thing:

        "When I choose "two" I would like that the choosen background item will be in the same color ( red ). In this example the choosen item text is "one" ( I think about that "one" above beacuse there are 2 x "one" )."

        Do you know how Can I do that? Maybe slot to signal currentIndexChanged(int index)? But how can I get the color of index "index"?

        EDIT I try this:

        box->model()->setData(idx, QColor(255,0,0), Qt::BackgroundColorRole);
        QColor color = (QColor)box->model()->data(idx,Qt::BackgroundColorRole);
        

        But it doesn't work.

        mrjjM 1 Reply Last reply
        0
        • T TomNow99

          @mrjj Thank you :)
          This is exactly what I need.

          But I Edit my post, so I need one more thing:

          "When I choose "two" I would like that the choosen background item will be in the same color ( red ). In this example the choosen item text is "one" ( I think about that "one" above beacuse there are 2 x "one" )."

          Do you know how Can I do that? Maybe slot to signal currentIndexChanged(int index)? But how can I get the color of index "index"?

          EDIT I try this:

          box->model()->setData(idx, QColor(255,0,0), Qt::BackgroundColorRole);
          QColor color = (QColor)box->model()->data(idx,Qt::BackgroundColorRole);
          

          But it doesn't work.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @TomNow99

          You mean the item when dropbox is not open?

          Its a lineEdit, so might need some fiddling. StyleSheet maybe.

          Yes to currentIndexChanged.
          You can use
          const QModelIndex idx = colorComboBox->model()->index(index,0); (or reverse. check doc)
          to get ModelIndex and read the color with Data function

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

            @mrjj

            Yes

            I have small problem with convert QVariant to QColor. But when I have to add parameter QVariant, QColor was fine :)

            mrjjM 1 Reply Last reply
            0
            • T TomNow99

              @mrjj

              Yes

              I have small problem with convert QVariant to QColor. But when I have to add parameter QVariant, QColor was fine :)

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @TomNow99
              QColor color = variant.value<QColor>();.

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

                @mrjj Perfect! :) Thank you!

                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