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

Why QLineEdit's setInputMask is removing spaces?



  • Hi All,

    I am using QLineEdit element and used inputMask on it as –
    inputMask : “nnnnnnnnnn”

    but if input string is-“abc 12” then i am getting it as – “abc12” , without spaces.

    why this behavior is occurring? and how to fix it?

    Regards,
    Meraj



  • Maybe "nnnnnnn" is only for alphanumeric characters, spaces are not included in that group, maybe you could use a QValidator instead ( qLineEdit.setValidator(...) )



  • When running the example below I get "abc 12" exactly, including the space. This is using Qt 4.7.2 on Windows. Which Qt version are you using? Does the example below reproduce the problem you are having? If not, can you modify it so that it does?

    @int main(int argc, char** argv)
    {
    QApplication app(argc, argv);
    QLineEdit edit;
    edit.setInputMask("nnnnnnnnnn");
    edit.setText("abc 12");
    edit.show();
    return app.exec();

    }@



  • To cite the docs:

    bq. The blank characters are always removed from the text after editing.



  • sigrid

    use this program-

    @#include <QtGui>
    int main(int argc, char** argv)
    {
    QApplication app(argc, argv);
    QLineEdit edit;
    edit.setInputMask("nnnnnnnnnn");
    edit.setText("abc 12");
    QString str = edit.text(); // here str is abc12
    edit.show();
    return app.exec();
    }@

    EDIT: added @ tags by VCsala



  • The line edit shows the space from the setText call, but it returns the input without spaces. That conforms to the specification in the docs.



  • Thanks for modifying the example. This is the expected behavior of setInputMask(). Space is the default blank character and the blank characters are always removed from the text after editing, as Volker pointed out.

    If you want to save the spaces then you should call setValidator () on the line edit and use a QRegExpValidator instead. This will allow you to have better control of what the text looks
    like. See the documentation:

    http://doc.trolltech.com/4.7/qlineedit.html#setValidator
    http://doc.trolltech.com/4.7/qregexpvalidator.html


Log in to reply