PDA

View Full Version : setBackgroundRole of parent without affecting children



TEAmerc
7th October 2015, 19:16
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:



QPalette mainPalette = palette();
mainPalette.setColor(backgroundRole(), QColor(1, 2, 3));
setPalette(mainPalette);
setAutoFillBackground(true);

wysota
8th October 2015, 23:03
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.

TEAmerc
9th October 2015, 14:27
This worked, Thanks!