How to make a QLineEdit not editable in Windows

Devolus picture Devolus · May 28, 2014 · Viewed 27.4k times · Source

I'm using Qt 5.2 and I would like to make a QLineEdit not editable. The problem with this is, that it doesn't appear like it. When using setReadOnly(true) it stays with white background and looks like it is still editable.

If I disable it, then it turns gray and the text also gets a lighter gray. The problem is, that one can not copy the text from it, in a disabled state.

So how can I make a QLineEdit properly non-editable and also make it look like it. In Windows such a control is usually gray, but the text stays black. Of course I could set the style manually, but this means that it is hard-coded and may look wrong on other platforms.

Answer

Nejat picture Nejat · May 28, 2014

After making the line edit readonly, you can set the background and text colors to whatever you like :

ui->lineEdit->setReadOnly(true);

QPalette *palette = new QPalette();
palette->setColor(QPalette::Base,Qt::gray);
palette->setColor(QPalette::Text,Qt::darkGray);
ui->lineEdit->setPalette(*palette);