[Re-ReOpened) Easy input mask



  • 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


  • Qt Champions 2016

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


  • Moderators

    Hi! QDateEdit is your friend ;-)



  • 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


  • Qt Champions 2016

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


  • you can use a QRegularExpressionValidator

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

  • Moderators

    @yczo said:

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

    ... numbers and characters:

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


  • Moderators

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


  • 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



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


  • Moderators

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



  • 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


  • Moderators

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



  • 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


  • Moderators

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


  • thank you that was great.



  • 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


  • Moderators

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



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


  • 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


  • Moderators

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

    A[0-3][0-7]
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.