Results 1 to 3 of 3

Thread: How to call QValidator derived object

  1. #1
    Join Date
    Jan 2007
    Posts
    91
    Thanks
    21
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Wink How to call QValidator derived object

    I have a QLineEdit field which I need to have the user either enter 0 characters or 10 characters and nothing in between. I have written a QValidator class that returns invalid if anything other than 0 or 10 characters are entered. It also returns intermediate other times.

    How do I prevent the user from moving out of the QLineEdit field until the field is correct?

    Here's my code snippets:
    (validate routine from QValidator derived class)
    Qt Code:
    1. QValidator::State ValidatorWepKey::validate(QString &input, int &pos) const
    2. {
    3. if (input.length() == 0)
    4. {
    5. return Acceptable;
    6. }
    7. else if (input.length() < m_minLength)
    8. {
    9. return Intermediate;
    10. }
    11. else if (input.length() == m_minLength)
    12. {
    13. return Acceptable;
    14. }
    15. else
    16. {
    17. return Invalid;
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    Main code:
    Qt Code:
    1. QLineEdit m_pKey1 = new QLineEdit();
    2. m_pKey1->setValidator(new ValidatorWepKey(10, this)); // should prevent
    To copy to clipboard, switch view to plain text mode 

    When do I call the ValidatorWepKey.validate()? Is that when I get the signal that the user is attempting to exit the field? How do I keep them in the field until it is correct?

    - BRC
    Last edited by wysota; 23rd March 2007 at 23:50. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to call QValidator derived object

    Using an input mask might simplify your task a bit. Although forcing the focus to the line edit until a correct input has been entered might not be a good idea. Consider disabling the ways for proceeding or something like that instead.
    J-P Nurmi

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

    bruccutler (23rd March 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    91
    Thanks
    21
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: How to call QValidator derived object

    I did have a slot that responded when they entered incorrect information and set the focus back to the line edit, but it wasn't keeping them there. I'll go back to that approach of not letting them change the focus until the entry is correct.

    So, can a mask enforce a minimum length?
    - Bruce

Similar Threads

  1. call bash/terminal command with QT
    By myta212 in forum Newbie
    Replies: 1
    Last Post: 13th November 2006, 15:38
  2. Call on Parent Object
    By hufgardm in forum Newbie
    Replies: 1
    Last Post: 7th September 2006, 15:11
  3. why cant i call setGeometry
    By freegnu in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2006, 04:59
  4. Passing Object to dll
    By ankurjain in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 09:50
  5. QProcess / system call not working under linux. Why?
    By johnny_sparx in forum Qt Programming
    Replies: 12
    Last Post: 11th March 2006, 00:32

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.