Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Wrong background color in custom QWidget, depending on where it is added
QtWS25 Last Chance

Wrong background color in custom QWidget, depending on where it is added

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            squan
            wrote on last edited by
            #5

            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

            artwawA 1 Reply Last reply
            0
            • S squan

              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

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #6

              @squan 10 years later

              You alright?

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved