Wrong background color in custom QWidget, depending on where it is added
-
In my GUI, I have a scrollable area defined like so:
@_scoreBoxScroll = new QScrollArea(this);
_scoreBoxScroll->setFrameShape(QFrame::NoFrame);@Some custom widgets are added later in a function:
@_scoreBoxWidget = new QWidget(this);
_scoreBoxWidgetLayout = new QHBoxLayout(_scoreBoxWidget);
_scoreBoxLayout->addWidget(_scoreBoxWidget);for (int i = 1; i <= _db->gamesPerRound(); ++i) {
GameWidget *newGame = new GameWidget(this, i, _db->playersString(MT::singular), _db->boogerScore());
_scoreBoxWidgetLayout->addWidget(newGame);
}_scoreBoxScroll->setWidget(_scoreBoxWidget);@
This results in a wrong background color for the GameWidgets:
!http://i.stack.imgur.com/lR1F8.png(Widgets with wrong background color)!When I add those widgets in the constructor with the very same code (and the _db calls replaced with static values, as when the constructor is called, there's no _db yet), the widgets are displayed with the correct color:
!http://i.stack.imgur.com/oK3kx.png(Widget with correct background color)!Why is a different color displayed when the widget is added by a function and not by the constructor? The whole code can be found at git://l3u.de/muckturnier.git, at the "work" branch.
Thanks in advance for all help!
-
Interestingly, when I replace the _db calls in the function with static values and I call it from the constructor, the widgets are also displayed with the correct color.
The function is called from another object using a pointer. If this call is used, I get the wrong color.
Why does it make a difference if the function is called by the object's constructor or from another object via a pointer?
-
I have created a minimalistic demo in the "demo" branch on git://l3u.de:muckturnier.git – I would be very glad if anyone could explain this behaviour!
-
Okay, I can answer my question myself now. It's due to the fact that QScrollArea::setWidget() calls setAutoFillBackground(true) on the added widget. When I add a manual
@_scoreBoxWidget->setAutoFillBackground(false);@
after the
@_scoreBoxScroll->setWidget(_scoreBoxWidget);@the background color is as expected.
-
Do you have a nested layout?
When nesting to QxBoxLayout, I found that I must not specify the same parent for widgets of the outer and inner layout, but better specify NO parent with the widget constructor calls, and then calling addWidget() on the two layouts will take care of proper parent child relations of the widgets.
I think this should be covered here, but it is not:
https://doc.qt.io/qt-6/qtwidgets-tutorials-widgets-nestedlayouts-example.html -
Do you have a nested layout?
When nesting to QxBoxLayout, I found that I must not specify the same parent for widgets of the outer and inner layout, but better specify NO parent with the widget constructor calls, and then calling addWidget() on the two layouts will take care of proper parent child relations of the widgets.
I think this should be covered here, but it is not:
https://doc.qt.io/qt-6/qtwidgets-tutorials-widgets-nestedlayouts-example.html