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
- #ifndef _edit_h
- #define _edit_h
- #include <qlineedit.h>
- Class CEdit: Public qlineedit {
- Q_object
- Public
- CEdit (Qwidget *parent);
- ~cedit ();
- enum Tagvalidatortype {
- Vtfloat,
- Vtint,
- Vtnovalidator,
- };
- void Updatevalidator (int type);
- Protected
- void Mousepressevent (Qmouseevent *event);
- void Focusinevent (Qfocusevent *event);
- Private
- int validatortype;
- bool Binit;
- };
- #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
- #include "Baseedit.h"
- #include <QRegExpValidator>
- Cedit::cedit (Qwidget *parent): Qlineedit (parent)
- {
- Setalignment (Qt::aligncenter);
- Validatortype = Vtnovalidator;
- Binit = false;
- }
- Cedit::~cedit ()
- {
- }
- void Cedit::updatevalidator (int type)
- {
- if (type! = Validatortype)
- {
- Validatortype = type;
- switch (type)
- {
- Case Vtfloat:
- {
- Qregexp Rx ("^ (-?[ 0]|-? [1-9] [0-9] {0,5}) (?:\ \.\\d{1,4})? $| (^\\t?$) ");
- Qregexpvalidator *preg = new Qregexpvalidator (Rx, this );
- Setvalidator (Preg);
- }
- Break ;
- Case Vtint:
- {
- 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?$) ");
- Qregexpvalidator *preg = new Qregexpvalidator (Rx, this );
- Setvalidator (Preg);
- }
- Break ;
- }
- }
- }
- void Cedit::mousepressevent (Qmouseevent *event)
- {
- if (binit)
- Binit = false;
- Else
- Qlineedit::mousepressevent (event);
- }
- void Cedit::focusinevent (Qfocusevent *event)
- {
- Qlineedit::focusinevent (event);
- QString str = text ();
- SetSelection (0, Str.length ());
- Binit = true;
- }
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
- int main (int argc, char *argv[])
- {
- Qapplication app (argc, argv);
- CEdit edit;
- Edit.updatevalidator (Cedit::vtint);
- Edit.resize (40, 120);
- Edit.show ();
- App.exec ();
- }
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)