Important: Please read the Qt Code of Conduct -

Want to make the QLIneEdit as readonly and color the text as grey to show it is disabled

  • Want to make the QLIneEdit as readonly and color the text as grey to show it is disabled

  • Lifetime Qt Champion

    So your question is actually :

    How to change the selection color for a QLIneEdit `?

  • Lifetime Qt Champion

    @Qt-Enthusiast Do you actually ever read the documentation?

  • Lifetime Qt Champion

  • Initial question
    I understood the initial question ("Want to make the QLIneEdit as readonly and color the text as grey to show it is disabled") as follows:

    • When the attribute readyOnly (in Qt Designer) has been set then the background color should be grey/gray - but it doesn't (also when autoFillBackground has been set). How to achieve that behaviour?

    I would expect that as standard behaviour that QLineEdit fields are greyed when they are read only. However, I created a custom widget derived from QLIneEdit that behaves accordingly.

    Desired behaviour
    If the LineEdit is setup like this
    then the line edit appears like this
    , otherwise setup like this
    it appears like this
    LineEdit_readOnly=false.png .

    Solution in C++

    To achieve this I created a class that is derived from QLineEdit. It just overrides the virtual method "event". I used the method "event" because it does not work in the constructor - and I could not find any better method to override. If you find a better one - please let me know. See also the code comments below.

    I also added some extra line to experiment.


    #ifndef LINEEDIT_H
    #define LINEEDIT_H
    #include <QLineEdit>
    class LineEdit : public QLineEdit
        LineEdit(const QString &contents, QWidget *parent = nullptr);
        LineEdit(QWidget *parent = nullptr);
        virtual bool event(QEvent *e);
    #endif // LINEEDIT_H


    #include "lineedit.h"
    #include <QEvent>
    LineEdit::LineEdit(const QString &contents, QWidget *parent) :
        QLineEdit::QLineEdit(contents, parent)
    LineEdit::LineEdit(QWidget *parent) :
        /* // The code below that is commented out does not work in this constructor.
        if ( ( isReadOnly() ) ) { // ... isReadOnly() does not provide the value that has been set in QtDesigner
            // qInfo("isReadOnly()==true"); // for debugging purpose only
            QPalette readOnlyPalette = palette();
            readOnlyPalette.setColor(QPalette::Base, readOnlyPalette.color(QPalette::Window));
    bool LineEdit::event(QEvent *pEvent)
        if ( (pEvent->type()) == QEvent::Polish ) {
            QPalette readOnlyPalette = palette();
        return QLineEdit::event(pEvent);


    The code works under...

    • Windows 10
    • C++
    • Qt Creator 4.13.2

    Further reading

  • @Joerg-Brueggmann
    Hi and welcome.

    The requirement can also be met by the one-liner from the last post in that stackoverflow link:

    setStyleSheet("QLineEdit[readOnly=\"true\"] {color: #808080; background-color: #F0F0F0;}");

    If you want this on all QLineEdits put it as a rule in the application stylesheet.

  • @JonB said in Want to make the QLIneEdit as readonly and color the text as grey to show it is disabled:

    setStyleSheet("QLineEdit[readOnly="true"] {color: #808080; background-color: #F0F0F0;}");

    Cuool! Or even Quool!

    The only drawback that I see is the color might not be compatible with "readOnlyPalette.color(QPalette::Window)".

  • @Joerg-Brueggmann
    That I don't know, you probably know more than I! My understanding is that once you use any stylesheet colors they override any palettes you might set in Qt styles. That's just how it is --- stylesheets are evaluated/act after everything else.

Log in to reply