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

[Re-ReOpened) Easy input mask

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 5 Posters 7.8k Views 2 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.
  • 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
                              • jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #21

                                For your example (max A37) it should be like this:

                                A[0-3][0-7]
                                

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                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