【发布时间】:2018-08-17 09:31:36
【问题描述】:
在 qt 中,您通常使用 QPalette 设置 QWidget 的颜色。
例子:
QPalette palette = new QPalette();
palette.setBrush(QPalette::Base, this->palette().backgorund());
QLineEdit *line = new QLineEdit();
line->setPalette(palette);
现在我有一个小问题。无法使用QPalette 更改 QLineEdit 的边框颜色。这意味着,我必须使用QStyleSheet。
例子:
QLineEdit *line = new QLineEdit();
line.setStyleSheet("border: 1px solid green");
但是现在我不能用QPalette 设置QLineEdit 的basecolor,因为QLineEdit 的背景颜色不再连接到QPalette::base。
这意味着,以下代码不会更改QLineEdit 的background-color:
QPalette palette = new QPalette();
palette.setBrush(QPalette::Base, this->palette().backgorund());
QLineEdit *line = new QLineEdit();
line->setPalette(palette);
line->setStyleSheet("border: 1px solid green");
但不可能在 StyleSheet 中定义 QLineEdit 的 background-color,因为 QLineEdit 的 background-color 必须是动态的。
我的问题:如何将QLineEdit 的背景色与QPalette::base 连接起来,以使用QPalette 动态定义QLineEdit 的background-color?
【问题讨论】:
-
为什么不能简单地创建/格式化包含所需背景和边框值的
QString? -
我不知道,你的意思。你的意思是,我应该创建一个继承自 QString 的类,其中包含一个背景字段和一个边框字段? - QString 中没有背景和边框属性,是吗?
标签: qt qtstylesheets qpalette