Qt限制文本框输入的方法(使用QRegExpValidator,为QLineEdit所独有)

在做界面编程的时候,对文本框的处理往往是个很头疼的事情,一是焦点进入文本框时,从人性化考虑,应选择文本框中文本,方便输入;二是,限制文本框的输入,只允许输入有效的文本,如果不这样做的话,那么就需要在程序中滤去非法输入。在这里介绍一种解决上述两个问题的方法:

[cpp] view plain copy
 
  1. #ifndef _EDIT_H  
  2. #define _EDIT_H  
  3.   
  4. #include <qlineedit.h>  
  5.   
  6. class CEdit : public QLineEdit {  
  7.   
  8.     Q_OBJECT  
  9. public:  
  10.     CEdit(QWidget *parent);  
  11.     ~CEdit();  
  12.     enum tagValidatorType {  
  13.         vtFloat,  
  14.         vtInt,  
  15.         vtNoValidator,  
  16.     };  
  17.     void updateValidator(int type);  
  18.   
  19. protected:  
  20.     void mousePressEvent(QMouseEvent *event);  
  21.     void focusInEvent(QFocusEvent *event);  
  22.   
  23. private:  
  24.     int validatorType;  
  25.     bool bInit;  
  26. };  
  27. #endif  

 CEdit派生自QLineEdit,重载focusInEvent,在这里实现选择文本框中的文本,解决上面的第一个问题;updateValidator方法实现限制文本框的输入,通过该函数,解决上面的第二个问题。下面我们来看实现:

[cpp] view plain copy
 
  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.   
  10. CEdit::~CEdit()  
  11. {  
  12.   
  13. }  
  14.   
  15. void CEdit::updateValidator(int type)  
  16. {  
  17.     if (type != validatorType)  
  18.     {  
  19.         validatorType = type;  
  20.         switch(type)  
  21.         {  
  22.         case vtFloat:  
  23.             {  
  24.                 QRegExp rx("^(-?[0]|-?[1-9][0-9]{0,5})(?:\.\d{1,4})?$|(^\t?$)");  
  25.                 QRegExpValidator *pReg = new QRegExpValidator(rx, this);  
  26.                 setValidator(pReg);  
  27.             }  
  28.             break;  
  29.         case vtInt:  
  30.             {  
  31.                 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?$)");  
  32.                 QRegExpValidator *pReg = new QRegExpValidator(rx, this);  
  33.                 setValidator(pReg);  
  34.             }  
  35.             break;  
  36.         }  
  37.     }  
  38. }  
  39.   
  40. void CEdit::mousePressEvent(QMouseEvent *event)  
  41. {  
  42.     if (bInit)  
  43.         bInit = false;  
  44.     else  
  45.         QLineEdit::mousePressEvent(event);  
  46. }  
  47.   
  48. void CEdit::focusInEvent(QFocusEvent *event)  
  49. {  
  50.     QLineEdit::focusInEvent(event);  
  51.     QString str = text();  
  52.     setSelection(0, str.length());  
  53.     bInit = true;  
  54. }  

首先来看updateValidator,当type=vtFloat时,浮点数的限制范围为[-999999.9999,999999.9999];当type=vtInt时,整数的输入范围为[1,65535]。大家也可以根据自己的要求,通过设置对应的正则表达式来设置自己的限制条件,如果你不懂正则表达式的话,可以baidu下,很简单的。好了我们再来看看focusInEvent,这个重载QLineEdit编辑框的焦点进入事件,在里面选择文本,这里的bInit变量是为了防止mousePressEvent把在focusInEvent里面选择的文本取消,因为焦点进入事件比mousePressEvent事件响应的早。

到这里,本文开头提的两个问题都解决了。下面是使用CEdit了:

[cpp] view plain copy
 
  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

一个更具体的实例:

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

原文地址:https://www.cnblogs.com/findumars/p/5615720.html