![](/img/trans.png)
[英]How to set text color in QLineEdit when background image is set for QlineEdit
[英]Qt Set Background Color of QLineEdit
我正在尝试更改QLineEdit
的背景颜色,但根本无法弄清。
我尝试使用最初是这样的stylesheets
QLineEdit *le = new QLineEdit();
le->setStyleSheet("background:#000;");
但这什么也没做。 我尝试像这样使用QPalette
QPalette palette;
palette.setColor(QPalette::Base, Qt::black);
palette.setColor(QPalette::Background, Qt::black);
le.setPalette(palette);
但这也不起作用。 我整天都在找东西,什么也找不到。 我是在做错什么,还是有另一种方法呢?
您可以通过设置如下调色板来设置线条编辑的背景和文本颜色:
QLineEdit *le = new QLineEdit();
QPalette palette;
palette.setColor(QPalette::Base,Qt::black);
palette.setColor(QPalette::Text,Qt::white);
le->setPalette(palette);
对我来说效果很好:
QLineEdit *le = new QLineEdit();
le->setStyleSheet("QLineEdit { background: rgb(0, 255, 255); selection-background-color: rgb(233, 99, 0); }");
您的代码几乎是正确的。 仅QLine编辑使用基础颜色。 因此,如果您不想替换可能包含边框填充和边距的现有样式表,并且只想更改背景,请使用QPalette:
QPalette palette = _ui->lnSearch->palette();
palette.setColor(QPalette::Base, Qt::green);
_ui->lnSearch->setPalette(palette);
感谢: https : //forum.qt.io/topic/64568/why-setting-background-color-of-qlineedit-has-no-effect
我不得不像这样使用标准CSS的背景色:
QLineEdit* edit = new QLineEdit();
edit->setStyleSheet("QLineEdit {background-color: black;}");
我正在使用Qt 5.4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.