How to create a childwidget with other color as parent
-
I want to make a black mainwindow with a yello label in the window, (not in designer-mode).
I tried this:mainWindow.setFixedSize(1100, 900); mainWindow.setPalette(QPalette(Qt::black)); QLabel* brett = new QLabel(&mainWindow); brett->setFixedSize(800, 800); brett->move(10, 10); brett->setPalette(QPalette(QPalette::Base, Qt::yellow)); brett->show(); mainWindow.show();
the mainwindow gets black, correctly, but the label is also black. (because its a child of the mainwindow
I tried this:
mainWindow.setFixedSize(1100, 900); mainWindow.setPalette(QPalette(Qt::black)); QLabel* brett = new QLabel(); brett->setFixedSize(800, 800); brett->move(10, 10); brett->setPalette(QPalette(QPalette::Base, Qt::yellow)); brett->show(); mainWindow.show();
now the label is yello, but its not a child of the mainwindow more, and so its showed in a new window.
How to change the Color of one Label, without changing the color of the parent?
-
@Lukas02
use "QPalette::Background" for the child widget -
You can use a stylesheet for your mainWindow to make the background black and another stylesheet to make your label yellow.
setStylesheet("QMainWindow#myWindow { background: black; }"); brett->setStylesheet("QLabel#brett { color: yellow; }");
brett->move(10, 10);
You usually want to put your label in a layout.
-
@raven-worx
i already tried this. But thxQLabel* brett = new QLabel(&mainWindow); brett->setFixedSize(800, 800); brett->move(10, 10); brett->setPalette(QPalette(QPalette::Background, Qt::yellow));
the same problem
-
@Lukas02 said in How to create a childwidget with other color as parent:
But I dont understand my mistake in my first version.
QWidget propagates explicit palette roles from parent to child. If you assign a brush or color to a specific role on a palette and assign that palette to a widget, that role will propagate to all the widget's children, overriding any system defaults for that role. Note that palettes by default don't propagate to windows (see isWindow()) unless the Qt::WA_WindowPropagation attribute is enabled.