Qt5 - 将背景颜色设置为QPushButton和QCheckBox
我试图在Qt5中更改QAbstractButton(QPushButton或QCheckBox)的背景颜色并且运气不错。Qt5 - 将背景颜色设置为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的抛出了其手中,并且描绘出afwul前瞻性块状按钮。
有一页标题为“How to change the background color of QWidget”,但它只是谈论这两种方法。
还有一个页面“Qt Style Sheets Examples”,这意味着如果你想改变背景颜色,你必须接管绘制按钮的所有方面,这似乎是矫枉过正。
我需要在Mac,Windows和Ubuntu Linux上运行它,如果我必须手动绘制关于按钮的所有内容(每个平台一次),那真的不是一件快乐的事情。
我错过了一些明显的东西吗?
p.s. “背景颜色”是指按钮周围的区域,而不是按钮正面文字的颜色。
我有同样的问题,但终于得到了这个工作。我使用Qt 5与融合的颜色主题:
QPalette pal = button->palette();
pal.setColor(QPalette::Button, QColor(Qt::blue));
button->setAutoFillBackground(true);
button->setPalette(pal);
button->update();
的准确顺序尝试如上这些命令,如果仍然不能正常工作,请将您的主题,融合,然后再试一次。
祝你好运!
我已经在linux和mac上试过了。奇怪的是,我使用的RGB QColor和Linux上它确实改变颜色,但没有指定的,在Mac上根本没有。我已经按照这个顺序尝试了上述命令。 – Lightbulb1
如果您在按钮上设置Flat(true),它将响应我的预期。虽然对其他人来说可能不是理想的结果。 – Lightbulb1
在样式表中添加边框:无;属性。出于某种原因,这也会删除默认的背景颜色。例如:background-color: rgba(46, 204, 113, 0.4); border: none;
3年后,我有与Qt 5.8,Windows 8相同的问题。这解决了它,即使它是一个奇怪的行为 – Phiber
更改对话框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)
}
试试这个:
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);
,这对我很有用。
你有没有解决这个问题? – Alchete
我没有......看起来如果你使用默认的小部件,Qt使用QStyle“绘制”它们,这很好,因为那么你的小部件在所有不同的平台上都是原生的,但不是很好,因为定制不再是可能的。你可能能够创建自己的QStyle派生类,并以某种方式搭载QStyle本身,但是我们决定在UI中完全不同的方向,绕过这个限制。 –