Qt text validator QIntValidator class and QDdoubleValidator class

QIntValidator class

The QIntValidator class provides a validator for ensuring that a string contains valid integers within a specified range.

Instructions:

1. Create and set up a validator
 	//构造一个验证器,该验证器接受从最小值100到最大值999的整数。
	QIntValidator *validator = new QIntValidator(100, 999, this); 
	//为lineEdit设置验证器
	ui.lineEdit->setValidator(validator);
	
	//可以使用:setRange()设置最小值和最大值,或使用setBottom()和setTop()单独设置
	//QIntValidator *validator = new QIntValidator(this)
	//validator->setRange(10,100);
	//validator->setBottom(12);
	//validator->setTop(50);
2. Determine whether the lineEdit input value is within the range of the validator
	int pos = 0;
	if (QValidator::Acceptable != ui.lineEdit->validator()->validate(ui.lineEdit->text(),pos) )
	{
    
    
		//输入值不在范围内
		QMessageBox::information(this,"tip","data range error");
	}

QDdoubleValidator class

QDoubleValidatorProvides range checking for floating-point numbers; including: upper limit, lower limit, and digits after the decimal point.
The method of use is the same as above, but it should be noted that it needs to setNotation()be sent by setting the flag of the floating point number.

	QDoubleValidator *doubleValidator=new QDoubleValidator(100.0, 999.0, 5, this);
	doubleValidator->setNotation(QDoubleValidator::StandardNotation); //设置为标准标记法,如:0.125
	ui.lineEdit->setValidator(doubleValidator);

Guess you like

Origin blog.csdn.net/yulong_abc/article/details/128989351