Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved How to set QComboBox background color with QPalette?

    General and Desktop
    2
    4
    3034
    Loading More Posts
    • 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
      Tapsa last edited by

      It seems that none of the colors in the palette of QComboBox are used to draw its background. Only the drop down triangle is changed to white with this code. (I have no entries yet in the boxes.)

      QPalette pal(palette());
      QBrush brush(Qt::white);
      pal.setBrush(QPalette::Window, brush);
      pal.setBrush(QPalette::Background, brush);
      pal.setBrush(QPalette::WindowText, brush);
      pal.setBrush(QPalette::Foreground, brush);
      pal.setBrush(QPalette::Base, brush);
      pal.setBrush(QPalette::AlternateBase, brush);
      pal.setBrush(QPalette::ToolTipBase, brush);
      pal.setBrush(QPalette::ToolTipText, brush);
      pal.setBrush(QPalette::Text, brush);
      pal.setBrush(QPalette::Button, brush);
      pal.setBrush(QPalette::ButtonText, brush);
      pal.setBrush(QPalette::BrightText, brush);
      pal.setBrush(QPalette::Base, brush);
      pal.setBrush(QPalette::Light, brush);
      pal.setBrush(QPalette::Midlight, brush);
      pal.setBrush(QPalette::Dark, brush);
      pal.setBrush(QPalette::Mid, brush);
      pal.setBrush(QPalette::Shadow, brush);
      pal.setBrush(QPalette::NoRole, brush);
      setPalette(pal);
      

      I tried to change the palette from designer too, but it appears that the background color of QComboBox is not defined in its palette, but determined elsewhere. How can I change the background color? If I use style sheet, QComboBox graphics reset to some really ugly look (since it has no default style sheet), but the background color changes.

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        Hi
        Please try this
        QPalette palette = ui->combo->palette();
        palette.setColor(QPalette::Active, QPalette::Button, Qt::white);
        palette.setColor(QPalette::Inactive, QPalette::Button, Qt::white);
        ui->combo->setPalette(palette);

        Also, a note on style sheets.
        If u use a style sheet, its a all or nothing thing. (sadly)

        So when u set something, it will loose its images etc so
        to change it, you must change most of it.

        1 Reply Last reply Reply Quote 1
        • T
          Tapsa last edited by

          I did that in the constructor of my sub classed QComboBox, but it changes nothing. Did you test if it works for you?

          1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion last edited by

            You are right. In win 7 it changes nothing.
            Sorry.

            1 Reply Last reply Reply Quote 1
            • First post
              Last post