PDA

View Full Version : set the validation for LineEdit



sangee
22nd August 2012, 13:13
I used keyboard design in my program.I want to set the values 18.5 to 39.0 only for keyboard's LineEdit.how to set the validation for this LineEdit?

Naahmi
22nd August 2012, 19:59
I used keyboard design in my program.I want to set the values 18.5 to 39.0 only for keyboard's LineEdit.how to set the validation for this LineEdit?

Here is a small Example: http://www.java2s.com/Code/Cpp/Qt/QDoubleValidatorandQLineEdit.htm

Use QdoubleValidator.

spirit
22nd August 2012, 20:16
Or this (http://qt-project.org/doc/qt-4.8/widgets-lineedits.html) and this (http://qt-project.org/doc/qt-4.8/widgets-spinboxes.html).

sangee
24th August 2012, 08:16
I was already saw this program.but doesn't work it.QDoubleValidator consider for decimal point only not ranges(18.5 to 39.0).how i set the validation for this LineEdit.

ChrisW67
26th August 2012, 08:26
QDoubleValidator checks the ranges. As documented, the line edit will not emit QLineEdit::editingFinished() or returnPressed() for invalid (in this case out-of-range) values. At any time QLineEdit::hasAcceptableInput() will return the current acceptability of the value. The validator does not stop you moving focus from the line edit with an intermediate value.

Since you clearly will not believe us, run this program and try 18.4, 18.6, 39.0 and 40 followed by Tab or Return in the top edit box and watch the output.


#include <QtGui>
#include <QDebug>

class Widget: public QWidget
{
Q_OBJECT
QLineEdit *line1;
public:
Widget(): QWidget() {
line1 = new QLineEdit(this);
QDoubleValidator *v = new QDoubleValidator(18.5, 39.0, 1, line1);
v->setNotation(QDoubleValidator::StandardNotation);
line1->setValidator(v);

QDoubleSpinBox *spin = new QDoubleSpinBox(this);
spin->setDecimals(1);
spin->setMinimum(18.0);
spin->setMaximum(39.0);


QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(line1);
layout->addWidget(spin);
setLayout(layout);

connect(line1, SIGNAL(textChanged(QString)), SLOT(slotTextChanged(QString)));
connect(line1, SIGNAL(returnPressed()), SLOT(checkIsValid()));
connect(line1, SIGNAL(editingFinished()), SLOT(checkIsValid()));
}

public slots:
void slotTextChanged(const QString &text) {
qDebug() << "Text changed to" << text << "and value is valid ==" << line1->hasAcceptableInput();
}
void checkIsValid() {
qDebug() << "Edit finished and value is valid ==" << line1->hasAcceptableInput();
}
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}
#include "main.moc"


What you really want is a QDoubleSpinBox which is more restrictive, just as easily edited, and always has a valid value.

sangee
1st September 2012, 10:18
Thank u very much.