Qt设置QLineEdi的背景色

2024-05-28 23:46:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图改变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);    

但这也没什么用。我找了一整天,什么也找不到。我做错什么了还是有别的办法?


Tags: lenewbaseqtblackbackgroundpalette背景色
3条回答

你的代码几乎是正确的。只有QLine edit使用基色。因此,如果不想替换现有的样式表(可以包含边框填充和边距),并且只想更改背景,请使用qpalete:

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

对我来说很好:

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

通过设置调色板,可以设置线条编辑的背景色和文本颜色,如下所示:

QLineEdit *le = new QLineEdit();

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

相关问题 更多 >

    热门问题