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: render whitespaces
Forum Updated to NodeBB v4.3 + New Features

QLineEdit: render whitespaces

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 983 Views 1 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.
  • J Offline
    J Offline
    JGROCHA
    wrote on last edited by
    #1

    Hi,

    I have a dialogue to enter PostgreSQL credentials, using QLineEdit.

    It is valid to have spaces in the username or on the database name, for example. So, I can not strip any whitespace from the user's input.

    But I would like to render whitespaces in a way that users know if they have have space characters at the beginning or end of the input.

    I've tried with stylesheets, but I can not set the background just for the the text entered. I can set the all background or the background of the selection.

    Another option would be to render the space character with another symbol, similar to a underscore, for example.

    Help is appreciated.

    1 Reply Last reply
    0
    • gde23G Offline
      gde23G Offline
      gde23
      wrote on last edited by
      #2

      What do you want to allow whitespace but render it with somehting else?
      I guess when you allow to enter a username containing whitespace but display it as _ or ¬ or whatever it will lead to more confusion compared to when you just do not allow it.

      J 1 Reply Last reply
      0
      • gde23G gde23

        What do you want to allow whitespace but render it with somehting else?
        I guess when you allow to enter a username containing whitespace but display it as _ or ¬ or whatever it will lead to more confusion compared to when you just do not allow it.

        J Offline
        J Offline
        JGROCHA
        wrote on last edited by JGROCHA
        #3

        @gde23 That's exactly what I want. Sometimes users just copy and paste the database name, with trailing spaces and complain about the failing access.

        I've tried setting font Underline property. but it is not clear (you hard notice the spaces at the beginning and at the end).

        qlineedit underline.png

        Using the unicode ␣ character to render the spaces would be awesome (https://www.compart.com/en/unicode/U+2423).

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

          Hi
          You could do like
          update: Hmm. Since we want space In the name, this is not a good solution.

          class MyLineEdit : public QLineEdit
          {
              Q_OBJECT
          public:
              explicit MyLineEdit(QWidget *parent = nullptr)
                  : QLineEdit(parent) {}
          protected:
              virtual void keyPressEvent(QKeyEvent *event) override
              {
                  if ( event->key() == Qt::Key_Space) {
                      QKeyEvent newEvent(QEvent::KeyPress, Qt::Key_Underscore, Qt::NoModifier, "_");
                      QLineEdit::keyPressEvent(&newEvent);
                      event->setAccepted(newEvent.isAccepted());
                  } else {
                      return QLineEdit::keyPressEvent(event);
                  }
              }
          };
          

          Which replaces space with _

          alt text

          To easy use in the app, you can use QCreators promote feature making it easy to replace existing LineEdits already placed on a form.
          https://doc.qt.io/qt-5/designer-using-custom-widgets.html

          Whoops. THis is not perfect as pasting to the linedit does not replace spaces.

          To fix that you might need something like
          https://forum.qt.io/topic/30162/how-disable-copy-and-paste-on-qlineedit

          But if the real issue is spaces in the start or the end, why not simply trim it before using it futher ?
          https://doc.qt.io/qt-5/qstring.html#trimmed

          JonBJ 1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            You could do like
            update: Hmm. Since we want space In the name, this is not a good solution.

            class MyLineEdit : public QLineEdit
            {
                Q_OBJECT
            public:
                explicit MyLineEdit(QWidget *parent = nullptr)
                    : QLineEdit(parent) {}
            protected:
                virtual void keyPressEvent(QKeyEvent *event) override
                {
                    if ( event->key() == Qt::Key_Space) {
                        QKeyEvent newEvent(QEvent::KeyPress, Qt::Key_Underscore, Qt::NoModifier, "_");
                        QLineEdit::keyPressEvent(&newEvent);
                        event->setAccepted(newEvent.isAccepted());
                    } else {
                        return QLineEdit::keyPressEvent(event);
                    }
                }
            };
            

            Which replaces space with _

            alt text

            To easy use in the app, you can use QCreators promote feature making it easy to replace existing LineEdits already placed on a form.
            https://doc.qt.io/qt-5/designer-using-custom-widgets.html

            Whoops. THis is not perfect as pasting to the linedit does not replace spaces.

            To fix that you might need something like
            https://forum.qt.io/topic/30162/how-disable-copy-and-paste-on-qlineedit

            But if the real issue is spaces in the start or the end, why not simply trim it before using it futher ?
            https://doc.qt.io/qt-5/qstring.html#trimmed

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

            @mrjj said in QLineEdit: render whitespaces:

            Whoops. THis is not perfect as pasting to the linedit does not replace spaces.

            That's what the OP says they want! I don't think they want to disable paste, rather support it with the space-to-underscore replacement? So they need to intercept the Paste event/signal and do replacement while pasting?

            But if the real issue is spaces in the start or the end, why not simply trim it before using it futher ?

            Quite agree. If that is all the OP really wants to accomplish then no need for anything more complicated.

            mrjjM 1 Reply Last reply
            1
            • JonBJ JonB

              @mrjj said in QLineEdit: render whitespaces:

              Whoops. THis is not perfect as pasting to the linedit does not replace spaces.

              That's what the OP says they want! I don't think they want to disable paste, rather support it with the space-to-underscore replacement? So they need to intercept the Paste event/signal and do replacement while pasting?

              But if the real issue is spaces in the start or the end, why not simply trim it before using it futher ?

              Quite agree. If that is all the OP really wants to accomplish then no need for anything more complicated.

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

              @JonB
              Hi
              Yeah I missed first-time that user would also paste their credentials and to catch that one has
              to catch the context menu AND ctrl+v (or similar) and also prevent pressing space at the beginning.

              So we want them to be able to type spaces if in the middle of the user name but not in the front/end so
              I think calling trimmed before login will do the trick unless i missed something more :)

              1 Reply Last reply
              2

              • Login

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