Results 1 to 6 of 6

Thread: Setting Background Color

  1. #1
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Setting Background Color

    I have several buttons on my application that have their background color set dynamically during runtime to reflect associated displayed data. This works fine - on some platforms. On others, the button background refuses to change. I'm not using stylesheets - I use the following construct:

    Qt Code:
    1. QPalette p = visibleButton->palette();
    2. p.setColor(QPalette::Button, newColor);
    3. visibleButton->setPalette(p);
    To copy to clipboard, switch view to plain text mode 

    I've also tried placing a small, blank label next to the button and changing it's background color using the same approach. This also fails on some platforms but succeeds on others.

    What is the recommended way of setting the background color of a button, or a widget in general, that will work everywhere?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Setting Background Color

    What is the recommended way of setting the background color of a button, or a widget in general, that will work everywhere?
    The approach you are taking is the correct one.
    But it could be that there is issue with the roles.
    Try to play with the color roles.
    If what you want is background, try using QPalette::Window and not QPalette::Button for example.
    It probably has to do with the following:
    The general button background color. This background can be different from Window as some styles require a different background color for buttons.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Setting Background Color

    I tried various combinations of QPalette::Window and QPalette::Button; still no joy.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Setting Background Color

    Hmm...
    Which platforms work and which don't?

    I would try stepping through the Qt code - and compare what is the difference between the working and the non working.
    It might be even a Qt bug.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Setting Background Color

    You might also try experimenting with QWidget::setAutoFillBackground(true/false)

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Setting Background Color

    I fiddled some more, and never had any luck with the button backgrounds. I suspect it requires stylesheets to make it work, and that's just too much effort for something as conceptually simple as 'setBackground(myColor)'. So I went back to the color swatch approach and got that working after a fair amount of fiddling.

    It was failing on Windows and on some Linux distros; other Linux distros worked just as I expected them to. So I don't think chasing platform-specific code variations is worthwhile, and it's something I just don't have time for anyway.

Similar Threads

  1. Setting the background color of a header in TableView
    By sunilqt in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2013, 13:06
  2. Replies: 1
    Last Post: 11th September 2009, 17:03
  3. setting background color and repositioning
    By mind_freak in forum Qt Programming
    Replies: 2
    Last Post: 11th August 2009, 06:22
  4. setting background color of QMessageBox
    By wagmare in forum Qt Programming
    Replies: 7
    Last Post: 23rd May 2009, 13:26
  5. Replies: 2
    Last Post: 10th March 2006, 20:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.