Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I tried using stylesheets originally like this

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

but that didn't do anything. I tried using QPalette like this

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?

like image 520
David Ludwig Avatar asked Dec 03 '14 23:12

David Ludwig


People also ask

How to change the background color of QLineEdit?

SOLVED how to change the background color of QLineEdit Try this : QPalette *palette = new QPalette(); palette->setColor(QPalette::Base,Qt::gray); ui->lineEdit->setPalette(*palette); Out of curiosity if our QLineEdit is disabled it should have the look and feel of your OS (in my case = grey) to reflect that.

How do I make QLineEdit not editable?

You can disable QLineEdit using method "setEnabled ( false )":http://qt-project.org/doc/qt-4.8/qwidget.html#enabled-prop which is inherited from QWidget.


1 Answers

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);
like image 73
Nejat Avatar answered Oct 04 '22 12:10

Nejat