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);

    Some custom widgets are added later in a function:
    @_scoreBoxWidget = new QWidget(this);
    _scoreBoxWidgetLayout = new QHBoxLayout(_scoreBoxWidget);

    for (int i = 1; i <= _db->gamesPerRound(); ++i) {
    GameWidget *newGame = new GameWidget(this, i, _db->playersString(MT::singular), _db->boogerScore());


    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
    after the

    the background color is as expected.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.