8

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?

cbuchart
  • 10,847
  • 9
  • 53
  • 93
user987013
  • 233
  • 2
  • 6
  • 18

7 Answers7

10

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

doubleValidator->setNotation(QDoubleValidator::StandardNotation);
Ed of the Mountain
  • 5,219
  • 4
  • 46
  • 54
soulia
  • 483
  • 2
  • 6
  • 20
5

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

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 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... – user987013 Apr 13 '12 at 03:09
  • 1
    See http://www.qtcentre.org/threads/50724-set-the-validation-for-LineEdit?p=227965#post227965 for more details on why its not working and *how* it should be used. – Ad N Sep 18 '13 at 15:01
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.

Wilmort
  • 294
  • 2
  • 15
1

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

Martin H.
  • 185
  • 2
  • 16
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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Samuel Harmer
  • 4,264
  • 5
  • 33
  • 67
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);
ignormies
  • 36
  • 2
  • 8
Ed of the Mountain
  • 5,219
  • 4
  • 46
  • 54
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....
Linh Dao
  • 1,305
  • 1
  • 19
  • 31