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. QLineEdit: How to show a processed text
Qt 6.11 is out! See what's new in the release blog

QLineEdit: How to show a processed text

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 3.1k Views 3 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.
  • l3u_L Offline
    l3u_L Offline
    l3u_
    wrote on last edited by
    #1

    Hi :-)

    A QLineEdit can show dots or stars instead of the entered text when one sets it to password mode. Is it also possible to display a custom processed text?

    My specific use case would be to show a small dot (or similar) instead of spaces so that they are visualized.

    As far as I can grasp it, one can only read the displayed text, but not set it and one can only set an echo mode from an existing enum. Apparently, the generation of the displayed text happens in non-public parts of the class, so I also can't override it.

    Is it possible to implement a custom echo mode? Thanks for all hints!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If I understand correctly you can do that using a proxy style:

      class ProxyStyle : public QProxyStyle
      {
          int styleHint(QStyle::StyleHint hint, const QStyleOption *option = nullptr, const QWidget *widget = nullptr, QStyleHintReturn *returnData = nullptr) const override
          {
              if (hint == QStyle::SH_LineEdit_PasswordCharacter) {
                  return QChar('$').unicode();
              }
              return QProxyStyle::styleHint(hint, option, widget, returnData);
          }
      };
      

      Set it on your QLineEdit and the echo char used will be a $ but you can change it to your liking.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      l3u_L 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        If I understand correctly you can do that using a proxy style:

        class ProxyStyle : public QProxyStyle
        {
            int styleHint(QStyle::StyleHint hint, const QStyleOption *option = nullptr, const QWidget *widget = nullptr, QStyleHintReturn *returnData = nullptr) const override
            {
                if (hint == QStyle::SH_LineEdit_PasswordCharacter) {
                    return QChar('$').unicode();
                }
                return QProxyStyle::styleHint(hint, option, widget, returnData);
            }
        };
        

        Set it on your QLineEdit and the echo char used will be a $ but you can change it to your liking.

        l3u_L Offline
        l3u_L Offline
        l3u_
        wrote on last edited by l3u_
        #3

        @SGaist Thanks for the hint with QProxyStyle, but I don't think this is what I need … this will only change the displayed character for the password display, won't it?

        I want e. g. to enter some text with spaces in between and what the QLineEdit should display would e. g. be some·text·with·spaces·in·between, simply the space characters replaced with some visible characters. But when requesting text(), I want to have the actually entered text, not the displayed one.

        1 Reply Last reply
        0
        • l3u_L Offline
          l3u_L Offline
          l3u_
          wrote on last edited by
          #4

          The Qt Linguist does this exact thing for the translation line edits … but I didn't find how they do it yet …

          mrjjM 1 Reply Last reply
          0
          • l3u_L l3u_

            The Qt Linguist does this exact thing for the translation line edits … but I didn't find how they do it yet …

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

            @l3u_ said in QLineEdit: How to show a processed text:

            Qt Linguist

            Hi
            You can look around here
            https://code.woboq.org/qt5/qttools/src/linguist/linguist/

            1 Reply Last reply
            2
            • l3u_L Offline
              l3u_L Offline
              l3u_
              wrote on last edited by
              #6

              Apparently, they don't use a QLineEdit, but a QTextEdit that is configured to edit just one line. Maybe they do it for a reason ;-)

              1 Reply Last reply
              3
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi
                For the fun of it i tried the obvious thing.

                class NoSpaceLineEdit : public QLineEdit
                {
                    Q_OBJECT
                public:
                    explicit NoSpaceLineEdit(QWidget *parent = nullptr) : QLineEdit(parent){}
                protected:
                    virtual void paintEvent(QPaintEvent *e) override
                    {
                        setText(text().replace(' ', '*'));
                        QLineEdit::paintEvent(e);
                    }
                };
                
                

                which does display * for space but also kills selection as setText resets it, so
                its a no go.

                1 Reply Last reply
                0
                • l3u_L Offline
                  l3u_L Offline
                  l3u_
                  wrote on last edited by l3u_
                  #8

                  I ended up with the following (a bit hacky ;-) solution (I used QPlainTextEdit anyway in my project, so I chose this one):

                  Edit: One also has to process pasted data so that no newline can be added

                  SpacesLineEdit.h:

                  #include <QPlainTextEdit>
                  
                  class SpacesLineEdit : public QPlainTextEdit
                  {
                      Q_OBJECT
                  
                  public:
                      explicit SpacesLineEdit(QWidget *parent = nullptr);
                  
                  protected:
                      virtual void keyPressEvent(QKeyEvent *event) override;
                      virtual QSize sizeHint() const override;
                      virtual QSize minimumSizeHint() const override;
                      virtual void insertFromMimeData(const QMimeData *source) override;
                  
                  private:
                      QSize m_sizeHint;
                  
                  };
                  

                  SpacesLineEdit.cpp:

                  #include "SpacesLineEdit.h"
                  #include <QLineEdit>
                  
                  SpacesLineEdit::SpacesLineEdit(QWidget *parent) : QPlainTextEdit(parent)
                  {
                      setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                      setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                      setLineWrapMode(QPlainTextEdit::NoWrap);
                      setTabChangesFocus(true);
                  
                      QTextOption option = document()->defaultTextOption();
                      option.setFlags(option.flags() | QTextOption::ShowTabsAndSpaces);
                      document()->setDefaultTextOption(option);
                  
                      // Stealing the sizeHint from a plain QLineEdit will do for now :-P
                      QLineEdit lineEdit;
                      m_sizeHint = lineEdit.sizeHint();
                  }
                  
                  QSize SpacesLineEdit::minimumSizeHint() const
                  {
                      return m_sizeHint;
                  }
                  
                  QSize SpacesLineEdit::sizeHint() const
                  {
                      return m_sizeHint;
                  }
                  
                  void SpacesLineEdit::keyPressEvent(QKeyEvent *event)
                  {
                      if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
                          event->ignore();
                          return;
                      }
                      QPlainTextEdit::keyPressEvent(event);
                  }
                  
                  void SpacesLineEdit::insertFromMimeData(const QMimeData *source)
                  {
                      QString text = source->text();
                      text.replace(QLatin1String("\r\n"), QLatin1String(" "));
                      text.replace(QLatin1Char('\n'), QLatin1String(" "));
                      text.replace(QLatin1Char('\r'), QLatin1String(" "));
                  
                      QMimeData processedSource;
                      processedSource.setText(text);
                      QPlainTextEdit::insertFromMimeData(&processedSource);
                  }
                  

                  Works fine … only question would be how to implement the sizeHint and minimalSizeHint in a "correct" way ;-) I couldn't adapt the implementation from Linguist … also, I couldn't take the functions from QLineEdit, as they access d pointer private stuff …

                  1 Reply Last reply
                  3

                  • Login

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