Results 1 to 3 of 3

Thread: How to change only the background color of widget to white?

  1. #1
    Join Date
    Oct 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    MacOS X Windows
    Thanks
    10

    Default How to change only the background color of widget to white?

    Hi,

    I'm making a color picker program for an RBG-LED and I found a color wheel online that I've been using. I decided later on to use a Tab Widget as my main widget, but I'm having some trouble with the background color of the color wheel. Since the tab widget's default background color is white and a "normal" widget's (which I've used as parent for the color wheel) default background color is grey, the color wheel ends up with a visible grey square around it (see attached picture).

    I tried writing this in my main, but it makes the whole widget white including the color wheel.
    colorWheelWidget->setStyleSheet("background-color:white;");

    The color wheel is written all in code, which I'm not very familiar with, so I wonder if there is a way to change the color wheel's default color to white inside the colorwheel cpp-file?

    I found the color wheel here:
    https://forum.qt.io/topic/19261/colorwheel

    //Leutzig
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: How to change only the background color of widget to white?

    In looking at the code, it appears that the outer square is filled with a brush created from the style's "Window" QPalette::ColorRole. So when you create your instance of the ColorWheel, retrieve the palette, change the brush for the Normal ColorGroup and Window ColorRole to white, then set the palette back on the widget:

    Qt Code:
    1. ColorWheel * myWheel = new ColorWheel( this );
    2. QPalette wheelPalette = myWheel->palette();
    3. wheelPalette.setBrush( QPalette::Normal, QPalette::Window, QBrush( Qt::white ) );
    4. myWheel->setPalette( wheelPalette );
    To copy to clipboard, switch view to plain text mode 

    This code sets the background color only for the active (normal) mode. The color for the inactive and disabled modes remain whatever they are. If you want the background color as white regardless of mode, then use the setBrush() method that takes only the ColorRole and QBrush as arguments.

  3. The following user says thank you to d_stranz for this useful post:

    Leutzig (28th November 2015)

  4. #3
    Join Date
    Oct 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    MacOS X Windows
    Thanks
    10

    Default Re: How to change only the background color of widget to white?

    Thank you so much d_stranz for this and the explanation, this did the trick!

Similar Threads

  1. cannot change QLCDNumber background color
    By saman_artorious in forum Qt Programming
    Replies: 2
    Last Post: 13th March 2013, 16:04
  2. Change background color of a custom widget
    By rubikon in forum Newbie
    Replies: 10
    Last Post: 5th July 2012, 13:21
  3. QML / widget - white background ??
    By migel in forum Newbie
    Replies: 0
    Last Post: 5th July 2011, 17:48
  4. how to change QTextEdit background color ?
    By mismael85 in forum Qt Programming
    Replies: 9
    Last Post: 26th June 2008, 22:05
  5. Change background color for a QPushButton?
    By Harvey West in forum Qt Programming
    Replies: 6
    Last Post: 5th January 2007, 14:23

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
  •  
Qt is a trademark of The Qt Company.