Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Want to make the QLIneEdit as readonly and color the text as grey to show it is disabled
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 10.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

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

    mrjjM jsulmJ 3 Replies Last reply
    1
    • Q Qt Enthusiast

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

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Qt-Enthusiast
      So your question is actually :

      How to change the selection color for a QLIneEdit `?

      1 Reply Last reply
      0
      • Q Qt Enthusiast

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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Qt-Enthusiast Do you actually ever read the documentation? http://doc.qt.io/qt-5/qlineedit.html#readOnly-prop

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • Q Qt Enthusiast

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Qt-Enthusiast http://doc.qt.io/qt-5/qwidget.html#styleSheet-prop
          http://doc.qt.io/qt-5/stylesheet.html

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • Joerg BrueggmannJ Offline
            Joerg BrueggmannJ Offline
            Joerg Brueggmann
            wrote on last edited by Joerg Brueggmann
            #5

            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

            JonBJ 1 Reply Last reply
            1
            • Joerg BrueggmannJ Joerg Brueggmann

              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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

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

              Joerg BrueggmannJ 1 Reply Last reply
              3
              • JonBJ JonB

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

                Joerg BrueggmannJ Offline
                Joerg BrueggmannJ Offline
                Joerg Brueggmann
                wrote on last edited by Joerg Brueggmann
                #7

                @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)".

                JonBJ 1 Reply Last reply
                0
                • Joerg BrueggmannJ Joerg Brueggmann

                  @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)".

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved