【问题标题】:Change background color in a specific widget of a Qt Stacked Widget在 Qt Stacked Widget 的特定小部件中更改背景颜色
【发布时间】:2015-03-26 14:39:55
【问题描述】:
我正在使用基于堆叠小部件的 Qt Creator 开发 Qt 应用程序。我想独立更改堆叠小部件每个页面的背景颜色(例如第一页蓝色,第二页红色等)。但是,当我将background-color: 添加到 Qt creator 上的 styleSheet 选项卡时,结果是堆叠小部件的所有页面都获得了该背景颜色。有没有办法为每个页面设置不同的背景颜色?
【问题讨论】:
标签:
qt
widget
qt-creator
qstackedwidget
【解决方案1】:
您可以为每个小部件执行此操作:
#page1 {
background-color: blue;
}
#page2 {
background-color: red;
}
#page1 和 #page2 是对象名称,请在 Qt Creator 的 Object Inspector 侧面板中找到它们。
【解决方案2】:
// yep, you can change it in constructor of your widget.
YourWidget::YourWidget(QWidget *parent):QWidget(parent),ui(new Ui::PageControl)
{
ui->setupUi(this);
QPalette background(palette());
background.setColor(QPalette::Background, Qt::black);
this->setAutoFillBackground(true);
this->setPalette(background);
}