Results 1 to 7 of 7

Thread: Regular Expression for QDate [YYYY/MM/DD] format

  1. #1
    Join Date
    Mar 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Regular Expression for QDate [YYYY/MM/DD] format

    Hi,
    I have a line edit which takes user input and stores it in a QDATE object in [YYYY/MM/DD] format. I want to construct a regular expression for this. and then use the QValidator to validate the user input against this expression.

    It would be helpful if you could give me a regular expression to validate QTIME [HH:MM:SS] for the same purpose.

    please help me out.
    Regards,
    BERA

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Regular Expression for QDate [YYYY/MM/DD] format

    For date try something like that

    Qt Code:
    1. QRegExp reDate("[0-9]{0,4}/[0-9]{0,2}/[0-9]{0,2}");
    To copy to clipboard, switch view to plain text mode 
    a life without programming is like an empty bottle

  3. The following user says thank you to zlatko for this useful post:

    bera82 (21st March 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Regular Expression for QDate [YYYY/MM/DD] format

    Maybe an input mask would be enough? If not, the easiest regexp validator for yyyy/mm/dd would be [12][0-9][0-9][0-9]/[01][0-9]/[0-3][0-9]. For time: [0-2][0-9]:[0-5][0-9]:[0-5][0-9]. Of course you can make them more complex so that dates like 2006-02-31 are not possible to enter. But it would probably be best to make a QDate based validator - subclass QValidator and reimplement validate() and fixup().

  5. The following user says thank you to wysota for this useful post:

    bera82 (21st March 2006)

  6. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Regular Expression for QDate [YYYY/MM/DD] format

    What about using QDateTimeEdit?

  7. #5
    Join Date
    Mar 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Regular Expression for QDate [YYYY/MM/DD] format

    Hi,

    I had used DateTimeEdit but the date format is not the way i need it. I need the date parameters as integers so that i can perform calculations. DateTimeEdit returns date as 11Mar2006! I tried to convert by applying the parameter "Qt::ISODate" but this parameter can be passed only for the "::fromString()" function !!

    Thanks,
    Regards,
    Bera.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Regular Expression for QDate [YYYY/MM/DD] format

    Quote Originally Posted by bera82
    DateTimeEdit returns date as 11Mar2006! I tried to convert by applying the parameter "Qt::ISODate" but this parameter can be passed only for the "::fromString()" function !!
    What about QDateTimeEdit::date(), QDateTimeEdit::dateTime() and QDateTimeEdit::time()?

  9. #7
    Join Date
    Jun 2019
    Posts
    2
    Qt products
    Qt5 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Windows Android Maemo/MeeGo

    Default Re: Regular Expression for QDate [YYYY/MM/DD] format

    Quote Originally Posted by jacek View Post
    The code below worked for this format, d/M/yyyy i.e 1/12/2019

    Qt Code:
    1. QRegExp R_date("([1-9]|[12][0-9]|3[01]{1,2})/([1-9]|[12]{1,2})/(19[0-9][0-9]|20[0-9][0-9])");
    2. QRegExpValidator *valida = new QRegExpValidator(R_date, this);
    3. ui->lineEdit->setValidator(valida);
    To copy to clipboard, switch view to plain text mode 

    The code below worked for this format, dd/MM/yyyy i.e 01/12/2019, 02/03/2019

    Qt Code:
    1. QRegExp R_date("(0[1-9]|[12][0-9]|3[01]{1,2})/(0[1-9]|[12]{1,2})/(19[0-9][0-9]|20[0-9][0-9])");
    2. QRegExpValidator *valida = new QRegExpValidator(R_date, this);
    3. ui->lineEdit->setValidator(valida);
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 3rd August 2019 at 22:41. Reason: missing [code] tags

Similar Threads

  1. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.