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. Why QLineEdit's setInputMask is removing spaces?
Forum Updated to NodeBB v4.3 + New Features

Why QLineEdit's setInputMask is removing spaces?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 9.3k 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.
  • M Offline
    M Offline
    Meraj Ahmad Ansari
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • D Offline
      D Offline
      diegosarmentero
      wrote on last edited by
      #2

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

      Diego Sarmentero
      Blog: http://diegosarmentero.com.ar
      Twitter: http://twitter.com/diegosarmentero

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sigrid
        wrote on last edited by
        #3

        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();

        }@

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          To cite the docs:

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

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Meraj Ahmad Ansari
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              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.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sigrid
                wrote on last edited by
                #7

                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

                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