简体   繁体   English

自定义QWidget中的背景颜色错误

[英]Wrong background color in custom QWidget

In my GUI, I have a scrollable area and a widget to be displayed in it defined like so: 在我的GUI中,我有一个可滚动区域和要显示在其中的小部件,如下所示:

_scoreBoxScroll = new QScrollArea(this);
_scoreBoxScroll->setFrameShape(QFrame::NoFrame);
_scoreBoxWidget = new QWidget(this);
_scoreBoxWidgetLayout = new QHBoxLayout(_scoreBoxWidget);

Some custom widgets are added later in a function: 稍后在函数中添加一些自定义窗口小部件:

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

_scoreBoxScroll->setWidget(_scoreBoxWidget);

This results in a wrong background color for the GameWidgets: 这将导致GameWidgets的背景颜色错误: 小部件的背景色错误

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: 当我用完全相同的代码将这些小部件添加到构造函数中时(并且_db调用替换为静态值,就像调用构造函数时一样,还没有_db),这些小部件将以正确的颜色显示: 具有正确背景颜色的小部件

In case this is interesting: the whole code can be found in git://l3u.de/muckturnier.git, the posted code resides in ScorePage/ScorePage.cpp. 如果这很有趣:可以在git://l3u.de/muckturnier.git中找到整个代码,发布的代码位于ScorePage / ScorePage.cpp中。

Why is a different color displayed here? 为什么在这里显示不同的颜色? And how can I fix this? 我该如何解决呢? Thanks in advance for all help! 在此先感谢您提供的所有帮助!

Edit: the code in the constructor used in the second example is (as I don't have _db there): 编辑:第二个示例中使用的构造函数中的代码是(因为我那里没有_db):

_scoreBoxWidget = new QWidget(this);
_scoreBoxWidgetLayout = new QHBoxLayout(_scoreBoxWidget);
_scoreBoxLayout->addWidget(_scoreBoxWidget);

for (int i = 1; i <= 2; ++i) {
    GameWidget *newGame = new GameWidget(this, i, QString::fromUtf8("Paar"), 21);
    _scoreBoxWidgetLayout->addWidget(newGame);
}

_scoreBoxScroll->setWidget(_scoreBoxWidget);

Edit: 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! 编辑:我已经在git://l3u.de:muckturnier.git的“ demo”分支中创建了一个简约的演示 –如果有人可以解释这种行为,我将非常高兴!

Okay, I can answer my question myself now. 好的,我现在可以自己回答问题。 It's due to the fact that QScrollArea::setWidget() calls setAutoFillBackground(true) on the added widget. 这是由于QScrollArea :: setWidget()在添加的小部件上调用setAutoFillBackground(true)的事实。 When I add a manual 当我添加手册时

_scoreBoxWidget->setAutoFillBackground(false);

after the 之后

_scoreBoxScroll->setWidget(_scoreBoxWidget);

the background color is as expected. 背景颜色符合预期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM