QT Limit text box Input method (using Qregexpvalidator, unique to Qlineedit)

Source: Internet
Author: User

In the interface programming, the processing of the text box is often a very headache, one is the focus into the text box, from the human consideration, should choose text box text, convenient input; second, limit the text box input, only allow the input of valid text, if not, then you need to filter out illegal input in the program. Here's a way to solve both of these problems:

[CPP]View PlainCopy
  1. #ifndef _edit_h
  2. #define _edit_h
  3. #include <qlineedit.h>
  4. Class CEdit: Public qlineedit {
  5. Q_object
  6. Public
  7. CEdit (Qwidget *parent);
  8. ~cedit ();
  9. enum Tagvalidatortype {
  10. Vtfloat,
  11. Vtint,
  12. Vtnovalidator,
  13. };
  14. void Updatevalidator (int type);
  15. Protected
  16. void Mousepressevent (Qmouseevent *event);
  17. void Focusinevent (Qfocusevent *event);
  18. Private
  19. int validatortype;
  20. bool Binit;
  21. };
  22. #endif

CEdit derives from Qlineedit, overloading focusinevent, where the text in the selection text box is implemented to solve the first problem above, and the Updatevalidator method implements restricting the input of the text box, through which the function solves the second problem above. Let's look at the implementation:

[CPP]View PlainCopy
  1. #include "Baseedit.h"
  2. #include <QRegExpValidator>
  3. Cedit::cedit (Qwidget *parent): Qlineedit (parent)
  4. {
  5. Setalignment (Qt::aligncenter);
  6. Validatortype = Vtnovalidator;
  7. Binit = false;
  8. }
  9. Cedit::~cedit ()
  10. {
  11. }
  12. void Cedit::updatevalidator (int type)
  13. {
  14. if (type! = Validatortype)
  15. {
  16. Validatortype = type;
  17. switch (type)
  18. {
  19. Case Vtfloat:
  20. {
  21. Qregexp Rx ("^ (-?[ 0]|-? [1-9] [0-9] {0,5}) (?:\ \.\\d{1,4})? $|  (^\\t?$) ");
  22. Qregexpvalidator *preg = new Qregexpvalidator (Rx, this );
  23. Setvalidator (Preg);
  24. }
  25. Break ;
  26. Case Vtint:
  27. {
  28. Qregexp Rx ("^ ([1-9][0-9]{0,3}|[ 1-5][0-9]{0,4}| [1-6] [0-4] [0-9] {0,3}| [1-6] [0-5] [0-4] [0-9] {0,2}| [1-6] [0-5] [0-5] [0-2] [0-9] {0,1}| [1-6] [0-5] [0-5] [0-3] [0-5]) $|  (^\\t?$) ");
  29. Qregexpvalidator *preg = new Qregexpvalidator (Rx, this );
  30. Setvalidator (Preg);
  31. }
  32. Break ;
  33. }
  34. }
  35. }
  36. void Cedit::mousepressevent (Qmouseevent *event)
  37. {
  38. if (binit)
  39. Binit = false;
  40. Else
  41. Qlineedit::mousepressevent (event);
  42. }
  43. void Cedit::focusinevent (Qfocusevent *event)
  44. {
  45. Qlineedit::focusinevent (event);
  46. QString str = text ();
  47. SetSelection (0, Str.length ());
  48. Binit = true;
  49. }

First of all, Updatevalidator, when type=vtfloat, the limit range for floating-point numbers is [ -999999.9999,999999.9999]; when type=vtint, the input range of integers is [1,65535]. You can also according to their own requirements, by setting the corresponding regular expression to set their own restrictions, if you do not understand the regular expression, you can Baidu, very simple. Okay, let's take a look at Focusinevent, this overload qlineedit the focus of the edit box into the event, select the text inside, and here the Binit variable is to prevent mousepressevent the text that is selected in the Focusinevent is canceled, Because the focus entry event responds earlier than the Mousepressevent event.

Here, the two issues mentioned at the beginning of this article are resolved. Here is the use of CEdit:

[CPP]View PlainCopy
    1. int main (int argc, char *argv[])
    2. {
    3. Qapplication app (argc, argv);
    4. CEdit edit;
    5. Edit.updatevalidator (Cedit::vtint);
    6. Edit.resize (40, 120);
    7. Edit.show ();
    8. App.exec ();
    9. }

http://blog.csdn.net/rabinsong/article/details/8932713

A more specific example:

http://blog.csdn.net/rabinsong/article/details/8997181

qt Restricted text box Input method (using Qregexpvalidator, unique to Qlineedit)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.