要注意validor的作用域,如果超出作用域,则会无效。例如下面的代码,在UI的类的构造函数里。所以要new一个validtor.
QIntValidator *intValidator = new QIntValidator; intValidator->setRange(1, 10); ui.lineEdit->setValidator(intValidator);
或者
myLineEdit->setValidator( new QIntValidator(0, 100, this) );