Qt提供了三个内置验证器类: QDoubleValidator, QIntValidator, QRegExpValidator。

 

QDoubleValidator类: 提供了对浮点数的范围检查.

 

QIntValidator类: 提供了一个确保一个字符串包含一个在一定有效范围内的整数的验证器

Example of use:

                QLineEdit *lineEdit;

                QIntValidator *validator = new QIntValidator(0,100,this)

                lineEdit->setValidator(validator )

//上述代码说明lineEdit只能输入0--100之间的数字。

 

QRegExpValidator类:提供了对满足正则表达的字符串的范围检查。

构造函数:

        QRegExpValidator ( QObject * parent, const char * name = 0 );

        QRegExpValidator ( const QRegExp & rx, QObject * parent, const char * name = 0 )

Example of use:

         QLineEdit *lineEdit;

         QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");

          QRegExpValidator *validator = new QRegExpValidator (regExp,this);

           lineEdit->setValidator(validator );

//上述代码说明lineEdit只能符合正则表达式 regExp("[A-Za-z][1-9][0-9]{0,2}")的字符串。

相关文章:

  • 2021-07-31
  • 2021-06-18
  • 2021-07-14
  • 2021-11-22
  • 2022-12-23
  • 2021-12-25
猜你喜欢
  • 2022-01-08
  • 2021-12-12
  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
相关资源
相似解决方案