【问题标题】:Qt5 - setting background color to QPushButton and QCheckBoxQt5 - 将背景颜色设置为 QPushButton 和 QCheckBox
【发布时间】:2014-03-08 06:26:59
【问题描述】:

我正在尝试更改 Qt5 中 QAbstractButton(QPushButton 或 QCheckBox)的背景颜色并且运气为零。

这无济于事:

pButton->setAutoFillBackground(true);
QPalette palette = pButton->palette();
palette.setColor(QPalette::Window, QColor(Qt::blue));
pButton->setPalette(palette);
pButton->show();

如果我尝试更改样式表:

pButton->setStyleSheet("background-color: rgb(255,255,0);");

然后 Qt 举起双手,画了一个看起来像块状的按钮。

有一个标题为“How to change the background color of QWidget”的页面,但它只是谈论这两种方法。

还有一个页面“Qt Style Sheets Examples”暗示如果要改变背景颜色,就得接管绘制按钮的方方面面,这似乎有点矫枉过正。

我需要它在 Mac、Windows 和 Ubuntu Linux 上运行,如果我必须手动绘制按钮的所有内容 3 次(每个平台一次),这真的不是一件快乐的事情。

我是否遗漏了一些明显的东西?

附言“背景颜色”是指按钮周围的区域,而不是按钮表面文字下方的颜色。

【问题讨论】:

  • 你解决过这个问题吗?
  • 我没有...似乎如果您使用默认小部件,Qt 会使用 QStyle “绘制”它们,这很好,因为您的小部件在所有不同平台上都显示为原生,但并非如此很好,因为定制不再可能。您也许可以创建自己的 QStyle 派生类并以某种方式搭载 QStyle 本身,但我们决定在我们的 UI 上采用完全不同的方向,绕过这个限制。

标签: qt qt5 qpushbutton qcheckbox


【解决方案1】:

我遇到了同样的问题,但最终解决了这个问题。我正在使用带有 Fusion 颜色主题的 Qt 5:

QPalette pal = button->palette();
pal.setColor(QPalette::Button, QColor(Qt::blue));
button->setAutoFillBackground(true);
button->setPalette(pal);
button->update();

按照上面的确切顺序尝试这些命令,如果仍然不起作用,请将您的主题设置为 Fusion 并重试。

祝你好运!

【讨论】:

  • 我在 linux 和 mac 上试过这个。很奇怪,我使用的是 RGB QColor,在 linux 上它确实改变了颜色,但不是指定的颜色,在 mac 上它根本没有。我已经按顺序尝试了上述命令。
  • 如果您在按钮上设置平面(true),它会响应我的期望。但对其他人来说可能不是一个理想的结果。
  • 在 PyQt 上,它设置为背景,但按钮绘制在背景上。导致非常奇怪的效果
【解决方案2】:

将属性border:none; 添加到样式表

由于某种原因,这也会删除默认背景颜色。

示例:background-color: rgba(46, 204, 113, 0.4); border: none;

【讨论】:

  • 3 年后,我在 Qt 5.8、Windows 8 上遇到了同样的问题。即使这是一个奇怪的行为也解决了
  • 这种方式对我来说是最简单的,并且在使用 Windows 7 的 Qt 5.12.4 创建者中工作。非常奇怪!
【解决方案3】:

更改 Dialog styleSheet 属性对我有用,将此属性设置为:

QPushButton:pressed {
    background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1,   stop:0 rgba(60, 186, 162, 255), stop:1 rgba(98, 211, 162, 255))
}
QPushButton {
     background-color: #3cbaa2; border: 1px solid black;
     border-radius: 5px;
}

QPushButton:disabled {
    background-color: rgb(170, 170, 127)
}

【讨论】:

  • 嗨,我有一个名为myButtonQPushButton,但myButton->pressed {myButton { backgroud-color ... 都不适用!请在示例中如何将这些功能用于QPushbutton
  • 你应该在 styleSheet 属性中使用 #myButton:pressed { 或 #myButton {
【解决方案4】:

试试这个:

QColor col = QColor(Qt::blue);
if(col.isValid()) {
   QString qss = QString("background-color: %1").arg(col.name());
   button->setStyleSheet(qss);
}

the QT Forum by @goetz 所述。

我使用了一些不同的 Qcolor col 定义为 QColor col = QColor::fromRgb(144,238,144);,这对我有用。

【讨论】:

    【解决方案5】:

    我找到了一个愚蠢的方法,尝试了调色板中的每个属性,并且在更改“QPalette::Base”时它对我有用。 或许你可以试一试。

        pButton->setAutoFillBackground(true);
        QPalette palette = pButton->palette();
        //palette.setColor(QPalette::Window, QColor(Qt.blue));
        //palette.setColor(QPalette::Foreground, QColor(Qt.blue));
        palette.setColor(QPalette::Base, QColor(Qt.blue));
        //palette.setColor(QPalette::AlternateBase, QColor(Qt.blue));
        //palette.setColor(QPalette::ToolTipBase, QColor(Qt.blue));
        //palette.setColor(QPalette::ToolTipText, QColor(Qt.blue));
        //palette.setColor(QPalette::Text, QColor(Qt.blue));
        //palette.setColor(QPalette::Button, QColor(Qt.blue));
        //palette.setColor(QPalette::ButtonText, QColor(Qt.blue));
        //palette.setColor(QPalette::BrightText, QColor(Qt.blue));
        pButton->setPalette(palette);
        pButton->show();
    

    参考链接:How to get a stylesheet property?

    【讨论】:

      【解决方案6】:

      我也遇到过同样的问题。您需要选择QPalette::Button 而不是QPalette::Window。 Qt 参考是这样说的: QPaletteButton - 一般按钮背景颜色。此背景可能与 Window 不同,因为某些样式需要按钮的不同背景颜色。 于是我就这样解决了:

              QPalette pal=this->palette(); \\"this" is my derived button class
              pal.setColor(QPalette::Window, style.background);
              QColor col=style.background; \\ my style wrapper, returns QColor
              this->setAutoFillBackground(true);
              this->setPalette(pal);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-31
        • 2014-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多