简体   繁体   English

Qt设置QLineEdit的背景色

[英]Qt Set Background Color of QLineEdit

I'm trying to change the background color of the QLineEdit and I can't figure it out at all. 我正在尝试更改QLineEdit的背景颜色,但根本无法弄清。

I tried using stylesheets originally like this 我尝试使用最初是这样的stylesheets

QLineEdit *le = new QLineEdit();
le->setStyleSheet("background:#000;");

but that didn't do anything. 但这什么也没做。 I tried using QPalette like this 我尝试像这样使用QPalette

QPalette palette;
palette.setColor(QPalette::Base, Qt::black);
palette.setColor(QPalette::Background, Qt::black);
le.setPalette(palette);    

but this didn't do anything either. 但这也不起作用。 I've been looking all day and can't find anything. 我整天都在找东西,什么也找不到。 am I doing something wrong or is there another way to do it? 我是在做错什么,还是有另一种方法呢?

You can set the background and text colors of line edit by setting the palette like : 您可以通过设置如下调色板来设置线条编辑的背景和文本颜色:

QLineEdit *le = new QLineEdit();

QPalette palette;
palette.setColor(QPalette::Base,Qt::black);
palette.setColor(QPalette::Text,Qt::white);
le->setPalette(palette);

Works fine for me: 对我来说效果很好:

QLineEdit *le = new QLineEdit();
le->setStyleSheet("QLineEdit { background: rgb(0, 255, 255); selection-background-color: rgb(233, 99, 0); }");

Your code is almost correct. 您的代码几乎是正确的。 Only QLine edit uses the Base color. 仅QLine编辑使用基础颜色。 So if you do not want to replace existing stylesheet which can contain borders padding and margin and you want to change background only, use QPalette: 因此,如果您不想替换可能包含边框填充和边距的现有样式表,并且只想更改背景,请使用QPalette:

QPalette palette = _ui->lnSearch->palette();
palette.setColor(QPalette::Base, Qt::green);
_ui->lnSearch->setPalette(palette);

Thanks to: https://forum.qt.io/topic/64568/why-setting-background-color-of-qlineedit-has-no-effect 感谢: https : //forum.qt.io/topic/64568/why-setting-background-color-of-qlineedit-has-no-effect

I had to use background-color from standard css like this: 我不得不像这样使用标准CSS的背景色:

QLineEdit* edit = new QLineEdit();
edit->setStyleSheet("QLineEdit {background-color: black;}");

I am using Qt 5.4 我正在使用Qt 5.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM