更改 Qt Stacked Widget 的特定小部件的背景颜色

Change background color in a specific widget of a Qt Stacked Widget

我正在使用基于堆叠小部件的 Qt Creator 开发 Qt 应用程序。我想独立更改堆叠小部件每个页面的背景颜色(例如,第一页蓝色,第二页红色等)。但是,当我将 background-color: 添加到 Qt creator 的 styleSheet 选项卡时,结果是堆叠小部件的所有页面都获得了该背景颜色。有没有办法为每个页面设置不同的背景颜色?

您可以为每个小部件执行此操作:

#page1 {
    background-color: blue;
}
#page2 {
    background-color: red;
}

其中 #page1#page2 是对象名称,在 Qt Creator 的对象检查器侧面板中找到它们。

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