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. [Re-ReOpened) Easy input mask
QtWS25 Last Chance

[Re-ReOpened) Easy input mask

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 5 Posters 7.4k Views
  • 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.
  • yczoY Offline
    yczoY Offline
    yczo
    wrote on last edited by yczo
    #1

    Hello everyone, I tryed to make a input mask kind "02-03-21".
    I searched on internet, but all examples are dificult for the first step.

    That is my idea, but don't works
    ui->lineEdit->setInputMask("nnn;");
    ui->lineEdit->setCursorPosition(0);

    I would appreciate some help.

    Greetings

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

      hi
      is that a date u want to have mask for ?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Hi! QDateEdit is your friend ;-)

        1 Reply Last reply
        0
        • yczoY Offline
          yczoY Offline
          yczo
          wrote on last edited by
          #4

          I need a custom mask, for example "0000 : 000 /234 1230 - 1234" to take different characters (numbers as characters too) and store all in a unique string.

          Greetings

          mrjjM ? 2 Replies Last reply
          0
          • yczoY yczo

            I need a custom mask, for example "0000 : 000 /234 1230 - 1234" to take different characters (numbers as characters too) and store all in a unique string.

            Greetings

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

            @yczo

            Hi you must define your input using the below symbols.
            "9999:9999/999 999 - 9999" for "0000 : 000 /234 1230 - 1234"
            But might need to use N also etc.
            So first you should think about what your format really is. the rules
            of it.

            Character	Meaning
            A	ASCII alphabetic character required. A-Z, a-z.
            a	ASCII alphabetic character permitted but not required.
            N	ASCII alphanumeric character required. A-Z, a-z, 0-9.
            n	ASCII alphanumeric character permitted but not required.
            X	Any character required.
            x	Any character permitted but not required.
            9	ASCII digit required. 0-9.
            0	ASCII digit permitted but not required.
            D	ASCII digit required. 1-9.
            d	ASCII digit permitted but not required (1-9).
            #	ASCII digit or plus/minus sign permitted but not required.
            H	Hexadecimal character required. A-F, a-f, 0-9.
            h	Hexadecimal character permitted but not required.
            B	Binary character required. 0-1.
            b	Binary character permitted but not required.
            >	All following alphabetic characters are uppercased.
            <	All following alphabetic characters are lowercased.
            !	Switch off case conversion.
            \	Use \ to escape the special characters listed above to use them as separators.
            
            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              you can use a QRegularExpressionValidator

              ui->lineEdit->setValidator(
              new QRegularExpressionValidator(
              QRegularExpression(
              // put here the regular expression that matches the valid input
              )
              ,ui->lineEdit
              )
              );
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1
              • yczoY yczo

                I need a custom mask, for example "0000 : 000 /234 1230 - 1234" to take different characters (numbers as characters too) and store all in a unique string.

                Greetings

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by A Former User
                #7

                @yczo said:

                for example "0000 : 000 /234 1230 - 1234"

                ... numbers and characters:

                ui->lineEdit->setInputMask("NNNN : NNNN /NNN NNN - NNNN");

                1 Reply Last reply
                1
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  ...or as suggested by @VRonin :

                  const QString a4 = "[0-9A-Za-Z]{4}";
                  const QString a3 = "[0-9A-Za-Z]{3}";
                  const QString expression = QString("%1\\s\\:\\s%1\\s\\/%2\\s%2\\s\\-\\s%1").arg(a4).arg(a3); 
                  QRegExp rx(expression);
                  QValidator *validator = new QRegExpValidator(rx, this);
                  ui->lineEdit->setValidator(validator);
                  
                  1 Reply Last reply
                  1
                  • yczoY Offline
                    yczoY Offline
                    yczo
                    wrote on last edited by
                    #9

                    Wow, that was great, thank you very much for your help. Have you not thought about doing a book with small examples of c ++ with qt and sell it on amazon.

                    Thank you very much

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      @Wieland QRegExp should be banished from new code. The only place I still use it over QRegularExpression is QSortFilterProxyModel::filterRegExp just because I don't have a choice. see http://doc.qt.io/qt-5/qregularexpression.html#notes-for-qregexp-users

                      @yczo you might be interested in: http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      ? 1 Reply Last reply
                      0
                      • VRoninV VRonin

                        @Wieland QRegExp should be banished from new code. The only place I still use it over QRegularExpression is QSortFilterProxyModel::filterRegExp just because I don't have a choice. see http://doc.qt.io/qt-5/qregularexpression.html#notes-for-qregexp-users

                        @yczo you might be interested in: http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf

                        ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        @VRonin Oh yes, right. Old habits die hard :-)

                        1 Reply Last reply
                        0
                        • yczoY Offline
                          yczoY Offline
                          yczo
                          wrote on last edited by yczo
                          #12

                          hello un thank you, A question more...

                          But with this code some fails. I can only the first validator a4 write, a3 don't work. The expresion dont work, only a4. Why might it be?

                          /    const QString a4 = "[0-9A-Za-Z]{4}";
                              const QString a3 = "[0-9A-Za-Z]{3}";
                              const QString expression = QString("%1\\s\\:\\s%1\\s\\/%2\\s%2\\s\\-\\s%1").arg(a4).arg(a3);
                              QRegExp rx(expression);
                              QValidator *validator = new QRegExpValidator(rx, this);
                              ui->lineEdit->setValidator(validator);
                          /your code here
                          

                          Thanks in advance

                          ? 1 Reply Last reply
                          0
                          • yczoY yczo

                            hello un thank you, A question more...

                            But with this code some fails. I can only the first validator a4 write, a3 don't work. The expresion dont work, only a4. Why might it be?

                            /    const QString a4 = "[0-9A-Za-Z]{4}";
                                const QString a3 = "[0-9A-Za-Z]{3}";
                                const QString expression = QString("%1\\s\\:\\s%1\\s\\/%2\\s%2\\s\\-\\s%1").arg(a4).arg(a3);
                                QRegExp rx(expression);
                                QValidator *validator = new QRegExpValidator(rx, this);
                                ui->lineEdit->setValidator(validator);
                            /your code here
                            

                            Thanks in advance

                            ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by A Former User
                            #13

                            @yczo You also need to enter the spaces, the colon, the slash and the dash.

                            1 Reply Last reply
                            0
                            • yczoY Offline
                              yczoY Offline
                              yczo
                              wrote on last edited by yczo
                              #14

                              Upssss....thank you very much, as could not see the extra characters, like in ui->lineEdit->setInputMask("NNN:NNNN /NNN NNN - NNNN;X");

                              is there a way to merge both? Because

                                  ui->setupUi(this);
                                 // ui->lineEdit->inputMask ;
                              
                                //  ui->getLine->setMaxLength(sizeMSG);
                                //  ui->lineEdit->setInputMask("nnn;A");
                                 // ui->lineEdit->setInputMask("nnn;");
                                //  ui->lineEdit->adjustSize();
                              
                              ui->lineEdit->setInputMask("NNN:NNN/NNN;X");
                              const QString a0 = "[0-9A-Za-Z]{3}";
                              const QString a1 = "[0-9A-Za-Z]{3}";
                              const QString a2 = "[0-9A-Za-Z]{3}";
                              const QString expression = QString("%1\\:\\%2\\/\\%3").arg(a0).arg(a1).arg(a2);
                              //ui->lineEdit->setCursorPosition(0);
                               //   const QString a4 = "[0-9A-Za-Z]{4}";
                               //   const QString a3 = "[0-9A-Za-Z]{3}";
                               //   const QString expression = QString("%1\\s\\:\\s%1\\s\\/%2\\s%2\\s\\-\\s%1").arg(a4).arg(a3);
                              
                                  QRegExp rx(expression);
                                  QValidator *validator = new QRegExpValidator(rx, this);
                                  ui->lineEdit->setValidator(validator);
                              
                                  ui->lineEdit->setFixedWidth(600);
                                  ui->lineEdit->setCursorPosition(0);
                              

                              don't works.

                              Greetings

                              ? 1 Reply Last reply
                              0
                              • yczoY yczo

                                Upssss....thank you very much, as could not see the extra characters, like in ui->lineEdit->setInputMask("NNN:NNNN /NNN NNN - NNNN;X");

                                is there a way to merge both? Because

                                    ui->setupUi(this);
                                   // ui->lineEdit->inputMask ;
                                
                                  //  ui->getLine->setMaxLength(sizeMSG);
                                  //  ui->lineEdit->setInputMask("nnn;A");
                                   // ui->lineEdit->setInputMask("nnn;");
                                  //  ui->lineEdit->adjustSize();
                                
                                ui->lineEdit->setInputMask("NNN:NNN/NNN;X");
                                const QString a0 = "[0-9A-Za-Z]{3}";
                                const QString a1 = "[0-9A-Za-Z]{3}";
                                const QString a2 = "[0-9A-Za-Z]{3}";
                                const QString expression = QString("%1\\:\\%2\\/\\%3").arg(a0).arg(a1).arg(a2);
                                //ui->lineEdit->setCursorPosition(0);
                                 //   const QString a4 = "[0-9A-Za-Z]{4}";
                                 //   const QString a3 = "[0-9A-Za-Z]{3}";
                                 //   const QString expression = QString("%1\\s\\:\\s%1\\s\\/%2\\s%2\\s\\-\\s%1").arg(a4).arg(a3);
                                
                                    QRegExp rx(expression);
                                    QValidator *validator = new QRegExpValidator(rx, this);
                                    ui->lineEdit->setValidator(validator);
                                
                                    ui->lineEdit->setFixedWidth(600);
                                    ui->lineEdit->setCursorPosition(0);
                                

                                don't works.

                                Greetings

                                ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #15

                                @yczo said:

                                is there a way to merge both?

                                Yes, just make the parts of the input optional, like this:

                                    ui->setupUi(this);
                                    ui->lineEdit->setInputMask("NNN : NNN");
                                
                                    const QString a0 = "[ 0-9A-Za-z]{3}"; // space before 0
                                    const QString space = "\\s";
                                    const QString colon = "\\:";
                                    const QString expression = QString("%1%2%3%2%1").arg(a0).arg(space).arg(colon);
                                    QRegExp rx(expression);
                                    QValidator *validator = new QRegExpValidator(rx, this);
                                    ui->lineEdit->setValidator(validator);
                                
                                1 Reply Last reply
                                2
                                • yczoY Offline
                                  yczoY Offline
                                  yczo
                                  wrote on last edited by yczo
                                  #16

                                  thank you that was great.

                                  1 Reply Last reply
                                  0
                                  • yczoY Offline
                                    yczoY Offline
                                    yczo
                                    wrote on last edited by
                                    #17

                                    The last question, ist posible set a max int value in the string (after other characters)?

                                    for example A37 (37 is the max),We could write from A00, A01.., to A37

                                    Greetings

                                    ? 1 Reply Last reply
                                    0
                                    • yczoY yczo

                                      The last question, ist posible set a max int value in the string (after other characters)?

                                      for example A37 (37 is the max),We could write from A00, A01.., to A37

                                      Greetings

                                      ? Offline
                                      ? Offline
                                      A Former User
                                      wrote on last edited by
                                      #18

                                      @yczo Yes, you can do this by adjusting the regular expression.

                                      1 Reply Last reply
                                      1
                                      • yczoY Offline
                                        yczoY Offline
                                        yczo
                                        wrote on last edited by
                                        #19

                                        Please, can you show me how to make it? Because I tried several options but, I have not luck.

                                        That is my idea

                                            ui->leStr->setInputMask("A99");
                                        
                                                                                //el conjunto para tanto espacios
                                            const QString id ="[ (A,M,O)]{1}";  //espacio delante para poder editar
                                            const QString cm ="[ 0-9]{2}";
                                        
                                            //const QString sl ="[0-50]{2}";
                                            const QString expr = QString("%1%2").arg(id).arg(cm);
                                            QRegExp cmp(expr);
                                            QValidator *vld = new QRegExpValidator(cmp,this);
                                            ui->leStr->setValidator(vld);
                                            ui->leStr->setCursorPosition(0);
                                        
                                        1 Reply Last reply
                                        0
                                        • yczoY Offline
                                          yczoY Offline
                                          yczo
                                          wrote on last edited by yczo
                                          #20

                                          That is another idea, but don't work too

                                          ui->le1->setInputMask("99");
                                          ui->le1->setValidator(new QDoubleValidator(01,10,1,this));
                                          ui->le1->setCursorPosition(0);
                                          

                                          or

                                          ui->le1->setInputMask("99");
                                          ui->le1->setValidator(new QIntValidator(1,10,this));
                                          ui->le1->setCursorPosition(0);
                                          

                                          i don't understand what I make wrong,

                                          Greetings

                                          Edit---
                                          ok i discovered that,

                                          ui->le1->setValidator(new QIntValidator(1,10,this));

                                          work good for integers, but, only in a separate field. how to make all together, that is another story und not idea XDD

                                          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