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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM