QDoubleValidator is not working?
Asked Answered
E

7

9

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?

Effy answered 12/4, 2012 at 7:31 Comment(3)
i dont know it offhead, but did you check that it doesnt allow exponential notation? i.e 100000e-4.Lantz
thanks Johan, i tried set a notation but it returns me erro 'setNotation' : is not a member of 'QDoubleValidator'...Effy
OHH and also its qt 4.2 im usingEffy
K
11

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

doubleValidator->setNotation(QDoubleValidator::StandardNotation);
Kalagher answered 10/10, 2014 at 19:19 Comment(0)
L
6

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.".

Lantz answered 12/4, 2012 at 9:8 Comment(2)
Hi johan, thanks for your answer, i have put emit return pressed before, and also ive try enter more number from a complete number such as "10.55", but im still able to add more like.100002121212121.55...Effy
See qtcentre.org/threads/… for more details on why its not working and how it should be used.Panel
M
1

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.

Maidservant answered 19/6, 2015 at 4:46 Comment(0)
N
1

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

Nickelodeon answered 24/3, 2016 at 12:42 Comment(0)
U
0

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?

Unrestrained answered 12/4, 2012 at 7:56 Comment(1)
no, validate returning that the input is not acceptable prevents invalid input iirc.Lantz
T
0

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);
Theater answered 22/10, 2014 at 13:2 Comment(0)
N
0

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....
Nixon answered 9/7, 2020 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.