Important: Please read the Qt Code of Conduct -

Disabled QLineEdit Background Color

  • Ok, I've searched around and keep finding the same solution:

    @MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    QLineEdit *edit;
    edit = new QLineEdit();

    QPalette pal = edit->palette();
    pal.setColor(QPalette::Disabled, QPalette::Text, pal.color(QPalette::Active, QPalette::Text));
    pal.setColor(QPalette::Disabled, QPalette::Base, pal.color(QPalette::Active, QPalette::Base));


    Actually, the above is what I got from the FAQs, but I've seen the solution in a few other threads here and there. However, my background color remains the same, grey. I've put in an RGB of 0xff, 0, 0 with the same results. If I take out the edit->setDisabled(true), I have an editable field with a red background. I'm using QT Creator 1.3.1 based on QT 4.6.2.

    Any Ideas? Thanks!

  • Try using QSS . Search for css in docs. Let me know if you fail to achieve.

  • Well, I didn't want to use the QSS, just so that I could keep it all in source(at least for now). It turns out I didn't do a few steps, for one, call setAutoFillBackground(true). That then filled in the background(not of the text, but of the line edit) red. So I changed p.setColor(QPalette::Base, Qt::white) to p.setColor(QPalette::Background, Qt::white) and it worked.

    I guess I misread the QPalette enum documentation. So it's solved.

Log in to reply