簡體   English   中英

Qt設置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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM