Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QLineEdit: render whitespaces



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



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



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


  • Lifetime Qt Champion

    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



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


  • Lifetime Qt Champion

    @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 :)


Log in to reply