QDoubleValidator is not working?

20.1k Views Asked by At

I'm trying to apply validator in a line edit box in Qt 4.2 and it is not working:

 QDoubleValidator *haha= new QDoubleValidator(this);
 haha->setBottom(0.00);
 haha->setDecimals(2);
 haha->setTop(100.00); 
 get_line_edit()->setValidator(haha);

or

 QDoubleValidator *haha= new QDoubleValidator(0.00,100.00,2,this);

Neither way, I can still enter what ever value I want.

But if I switch to QIntValidator, it works!

So I went onto Google and did a bit search, and many people used to having the same issue. Is it a bug? or there should be some other set up I should do?

7

There are 7 best solutions below

1
Samuel Harmer On

This example works fine in 4.8. It doesn't look like its changed since 4.2 so I suspect the problem lies in how you are creating your QLineEdit. This is the relevent code from that example.

QLineEdit* validatorLineEdit;
validatorLineEdit = new QLineEdit;
validatorLineEdit->setValidator( new QDoubleValidator(-999.0, 999.0, 2, validatorLineEdit));

How have you created your line edit?

2
Johannes Schaub - litb On

The validator documentation says that it returns "Intermediate" from "validate" when the input is an arbitrary double but out of range.

You need to distinguish intermediate input and the final input the user wants to submit by use of a line edit control (e.g. by emitting the "returnPressed" signal). If the user typed "10000" that is still a valid intermediate input for a number between 0 and 100 because the user can prefix this input with "0.".

0
soulia On

Just tripped over this one. Try setting the QDoubleValidator notation to:

doubleValidator->setNotation(QDoubleValidator::StandardNotation);
0
Ed of the Mountain On

To clarify, use QDoubleValidator::setNotation(QDoubleValidator::StandardNotation)

Example:

QDoubleValidator* doubleValidator = new QDoubleValidator(-999.0, 999.0, 2, validatorLineEdit);
doubleValidator->setNotation(QDoubleValidator::StandardNotation);
validatorLineEdit->setValidator(doubleValidator);
0
Wilmort On

You have to set notation to your validator

QLineEdit *firstX;
QDoubleValidator* validFirstX = new QDoubleValidator(-1000, 1000, 3, ui.firstX); 
validFirstX->setNotation(QDoubleValidator::StandardNotation);

then it works but not fully correct. Interesting part is that it controls the digit numbers not number itself. For example, In this example, you can enter to QLineEdit 1000 either 9999.

0
Martin H. On

&& ( input.toDouble() > top() || input.toDouble() < bottom())

0
Linh Dao On

If you set a validator to a QLineEdit then you can use the function hasAcceptableInput() to check whether inputed value is valid or invalid. For example:

if (!ui->lineEdit_planned_count_vrt->hasAcceptableInput())
{
    slot_show_notification_message("EDIT_PLAN_COUNT_VRT", notification_types::ERROR, INVALID_INPUTED_VALUE);
    return;
}
bool isOk = false;
double value = ui->lineEdit_planned_count_vrt->text().toDouble(&isOk);
//do something with the value here....