I am using Qt Creator 4.13.1 with Qt 5.15.1 on Windows 10 Pro.
I am new to using Stylesheets in Qt and a bit confused of the possible selectors considering QSplitter
and its child. I want to have 2 childs separated by a splitter-handle and draw their backgrounds in different colors.
This is an example szenario
QSplitter* splitter = new QSplitter();
QWidget* widgetA = new QWidget();
// add a layout with some further child widgets
QWidget* widgetB = new QWidget();
// add a layout with some further child widgets
splitter->addWidget(widgetA);
splitter->addWidget(widgetB);
layout()->addWidget(splitter);
where I tried:
widgetA->setStyleSheet("background-color: #ff0000;");
which applies to all child widgets ofwidgetA
(e.g. QLabels, QPushButtons, ...) , but not to their surroundingwidgetA
itselfsplitter->setStyleSheet("background-color: #ff0000;");
applies to thewidgetA
,widgetB
and the handle and all widgets belowwidgetA
andwidgetB
- naming
widgetA->setObjectName("Tim");
andsplitter->setStyleSheet("QWidget#Tim {background-color: #ff0000;}");
which has no effect at all. - [EDIT]
splitter->setStyleSheet("QSplitter QWidget #Andy {background-color: #ff0000;}");
applies the background only to a certain widget inside either child widget. But usingsplitter->setStyleSheet("QSplitter #Tim {background-color: #ff0000;}");
has (again) no effect at all.
How would I set the background-color
of widgetA
separately, so without affecting the childs of widgetA
or the other splitter child widgetB
?
I don't see any problems with using
widget->setStyleSheet()
in your case. Here, I have tested your implementation ofsetObjectName()
:Output