Results 1 to 3 of 3

Thread: setBackgroundRole of parent without affecting children

  1. #1
    Join Date
    Sep 2015
    Location
    Ontario, Canada
    Posts
    28
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default setBackgroundRole of parent without affecting children

    I have a QMainWindow with several other widgets contained within it (a QTableView, a QPixmap , a QGroupBox etc.) and I want to be able to change the background color of the parent widget without also changing the background color of its children such as the QTableView so that the window background is a different color, but the background of the table, groupbox, and other widgets beneath it remain with the default look.

    Is there any way to do this as currently the children all inherit the backgroundrole of the parent.

    I think it's probably unnecessary, but just in case, my current code to change the background color is below:

    Qt Code:
    1. QPalette mainPalette = palette();
    2. mainPalette.setColor(backgroundRole(), QColor(1, 2, 3));
    3. setPalette(mainPalette);
    4. setAutoFillBackground(true);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: setBackgroundRole of parent without affecting children

    You could explicitly set a color for the role you are going to change for immediate children of the widget you are going to modify before actually modifying the palette. You would then effectively cut off inheriting the color down the hierarchy.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    TEAmerc (9th October 2015)

  4. #3
    Join Date
    Sep 2015
    Location
    Ontario, Canada
    Posts
    28
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: setBackgroundRole of parent without affecting children

    This worked, Thanks!

Similar Threads

  1. Access to shared map between parent - children
    By atomic in forum Qt Programming
    Replies: 5
    Last Post: 10th August 2015, 22:24
  2. How to set an attribute without affecting children?
    By dominate in forum Qt Programming
    Replies: 0
    Last Post: 15th February 2011, 14:58
  3. Replies: 0
    Last Post: 14th April 2010, 15:03
  4. disable a parent and children in a tree model
    By qt_gotcha in forum Newbie
    Replies: 4
    Last Post: 9th July 2009, 06:49

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.