Why can’t the space in QVBoxLayout be set by the stylesheet, while only widgets like labels successfully set their background color?
-
I used setStyleSheet("QWidget{background:red}") in the widget, but the space in the VLayout is controlled by the parent widget above it. This is a multi-layered widget, and I’m not sure which level of the widget controls the color. I can only determine that the color setting at the lowest level has failed.Why is this the case?
-
In theory, the color of space in QVBoxLayout is controlled by the current widget, right?
A QVBoxLayout is not a widget, just an agent to arrange them on the screen. Space you see between widgets in a layout is from whatever lies underneath (i.e. the widget on which the layout (might be nested) is ultimately applied). That widget may not honour the "background" attribute or be displaying that color in the places you are overlaying.
We cannot see your "multi-layered widget", how your layouts apply or are nested, or exactly what pixels your query pertains to. It's hard to be specific about generalities.
#include <QApplication> #include <QWidget> #include <QPushButton> #include <QVBoxLayout> int main(int argc, char **argv) { QApplication app(argc, argv); QWidget w; w.setStyleSheet("QWidget { background-color: red }"); QVBoxLayout *layout = new QVBoxLayout(&w); for (int i = 0; i < 5; ++i) { QPushButton *button = new QPushButton("Demo", &w); layout->addWidget(button); } w.show(); return app.exec(); }
Produces what one might expect:
-
I used setStyleSheet("QWidget{background:red}") in the widget, but the space in the VLayout is controlled by the parent widget above it. This is a multi-layered widget, and I’m not sure which level of the widget controls the color. I can only determine that the color setting at the lowest level has failed.Why is this the case?
@John-Van Not sure I understand the problem: a widget can only apply a stylesheet on itself, it's not its job to alter its parent. Maybe it will help if you set margins to 0 to remove empty space around the widget inside the layout?
-
@John-Van Not sure I understand the problem: a widget can only apply a stylesheet on itself, it's not its job to alter its parent. Maybe it will help if you set margins to 0 to remove empty space around the widget inside the layout?
@jsulm
Yes, setSpacing (0) can solve this problem, but it may bring some other issues. In theory, the color of space in QVBoxLayout is controlled by the current widget, right?setStyleSheet("QWidget{background:red}"); auto v0 = new QVBoxLayout(this); v0->setSpacing(30);
-
In theory, the color of space in QVBoxLayout is controlled by the current widget, right?
A QVBoxLayout is not a widget, just an agent to arrange them on the screen. Space you see between widgets in a layout is from whatever lies underneath (i.e. the widget on which the layout (might be nested) is ultimately applied). That widget may not honour the "background" attribute or be displaying that color in the places you are overlaying.
We cannot see your "multi-layered widget", how your layouts apply or are nested, or exactly what pixels your query pertains to. It's hard to be specific about generalities.
#include <QApplication> #include <QWidget> #include <QPushButton> #include <QVBoxLayout> int main(int argc, char **argv) { QApplication app(argc, argv); QWidget w; w.setStyleSheet("QWidget { background-color: red }"); QVBoxLayout *layout = new QVBoxLayout(&w); for (int i = 0; i < 5; ++i) { QPushButton *button = new QPushButton("Demo", &w); layout->addWidget(button); } w.show(); return app.exec(); }
Produces what one might expect:
-
In theory, the color of space in QVBoxLayout is controlled by the current widget, right?
A QVBoxLayout is not a widget, just an agent to arrange them on the screen. Space you see between widgets in a layout is from whatever lies underneath (i.e. the widget on which the layout (might be nested) is ultimately applied). That widget may not honour the "background" attribute or be displaying that color in the places you are overlaying.
We cannot see your "multi-layered widget", how your layouts apply or are nested, or exactly what pixels your query pertains to. It's hard to be specific about generalities.
#include <QApplication> #include <QWidget> #include <QPushButton> #include <QVBoxLayout> int main(int argc, char **argv) { QApplication app(argc, argv); QWidget w; w.setStyleSheet("QWidget { background-color: red }"); QVBoxLayout *layout = new QVBoxLayout(&w); for (int i = 0; i < 5; ++i) { QPushButton *button = new QPushButton("Demo", &w); layout->addWidget(button); } w.show(); return app.exec(); }
Produces what one might expect:
-