Qt Set Background Color of QLineEdit

David Ludwig picture David Ludwig · Dec 4, 2014 · Viewed 33k times · Source

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?

Answer

Nejat picture Nejat · Dec 4, 2014

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);