Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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

    @Qt-Enthusiast
    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? http://doc.qt.io/qt-5/qlineedit.html#readOnly-prop


  • 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?

    Comment
    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
    readOnly=true.png
    then the line edit appears like this
    LineEdit_readOnly=true.png
    , otherwise setup like this
    readOnly=false.png
    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.

    lineedit.h

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

    lineedit.cpp

    #include "lineedit.h"
    
    #include <QEvent>
    
    LineEdit::LineEdit(const QString &contents, QWidget *parent) :
        QLineEdit::QLineEdit(contents, parent)
    {
    }
    
    LineEdit::LineEdit(QWidget *parent) :
        QLineEdit::QLineEdit(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));
            setPalette(readOnlyPalette);
        }*/
    }
    
    bool LineEdit::event(QEvent *pEvent)
    {
        if ( (pEvent->type()) == QEvent::Polish ) {
            QPalette readOnlyPalette = palette();
    
            readOnlyPalette.setColor(
                QPalette::Base, 
                readOnlyPalette.color(QPalette::Window));
            setPalette(readOnlyPalette);
        }
        return QLineEdit::event(pEvent);
    }
    

    Configuration

    The code works under...

    • Windows 10
    • C++
    • Qt Creator 4.13.2

    Further reading
    https://stackoverflow.com/questions/23915700



  • @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