Dav*_*wig 13 c++ qt background palette qlineedit
我正在尝试改变它的背景颜色,QLineEdit我根本无法弄明白.
我stylesheets最初尝试使用这样的
QLineEdit *le = new QLineEdit();
le->setStyleSheet("background:#000;");
Run Code Online (Sandbox Code Playgroud)
但那没有做任何事情.我尝试过QPalette像这样使用
QPalette palette;
palette.setColor(QPalette::Base, Qt::black);
palette.setColor(QPalette::Background, Qt::black);
le.setPalette(palette);
Run Code Online (Sandbox Code Playgroud)
但这也没有做任何事情.我一直在寻找,找不到任何东西.我做错了什么或有其他办法吗?
Nej*_*jat 11
您可以通过设置调色板来设置行编辑的背景和文本颜色:
QLineEdit *le = new QLineEdit();
QPalette palette;
palette.setColor(QPalette::Base,Qt::black);
palette.setColor(QPalette::Text,Qt::white);
le->setPalette(palette);
Run Code Online (Sandbox Code Playgroud)
对我来说很好:
QLineEdit *le = new QLineEdit();
le->setStyleSheet("QLineEdit { background: rgb(0, 255, 255); selection-background-color: rgb(233, 99, 0); }");
Run Code Online (Sandbox Code Playgroud)
你的代码几乎是正确的。只有 QLine 编辑使用基本颜色。因此,如果您不想替换可以包含边框填充和边距的现有样式表,并且只想更改背景,请使用 QPalette:
QPalette palette = _ui->lnSearch->palette();
palette.setColor(QPalette::Base, Qt::green);
_ui->lnSearch->setPalette(palette);
Run Code Online (Sandbox Code Playgroud)
感谢:https : //forum.qt.io/topic/64568/why-setting-background-color-of-qlineedit-has-no-effect