QDoubleValidate Switching



  • Hi everyone,

    i've been struggling with the validation of my QDoubleValidator. I've created a QDoubleValidator object, set the range and set it to be the Validator of a lineEdit object. This works just fine, but when I try to switch through the QValidator::State, I get a compiler error telling me that there is no matching function for call to the validate Method.

    The Code:

    switch(ui->lineEdit1->validator()->validate(ui->lineEdit1->text(), 0))
    {
    case QValidator::Acceptable: /* Do Something */ ;
    ...
    }
    

    The first line produces the compiler error:

    no matching function for call to 'QValidator::validate() const'   switch(ui->lineEdit1->validator()->validate())
    

  • Qt Champions 2016

    @Dan90
    Hello,
    The first line can't be producing that error, as the compiler is complaining about:

    switch(ui->lineEdit1->validator()->validate())
    

    Kind regards.



  • @kshegunov
    Hi,

    I don't get it. the first line of the Code i posted is

    switch(ui->lineEdit1->validator()->validate(ui->lineEdit1->text(), 0))
    

    and the compiler complains about

    switch(ui->lineEdit1->validator()->validate()
    

    which is exactly the first line. How is the first line not producing this error ?

    Regards,
    Daniel


  • Qt Champions 2016

    @Dan90
    Hello Daniel,

    ui->lineEdit1->validator()->validate()
    

    is different from:

    ui->lineEdit1->validator()->validate(ui->lineEdit1->text(), 0)
    

    Note that the compiler is complaining abut a function that's called without arguments. You could verify that by going to the exact line the compiler is issuing the error for, and check what method is invoked there.

    Kind regards.



  • @kshegunov

    Hi,

    I have now declared

    QString text = ui->lineEdit1->text();
    int pos = 0;
    

    and changed

    ui->lineEdit1->validator()->validate(ui->lineEdit1->text(), 0);
    

    to

    ui->lineEdit1->validator()->validate(text,pos);
    

    and it works perfectly. Can you tell me where the difference ist between declaring the variables first and then passing the variables to the function and directly passing the objects ?


  • Qt Champions 2016

    @Dan90
    Aha, yes, of course. I completely missed that, sorry. Notice that the parameters for QValidator::validate are references. So while ui->lineEdit1->text() you can pass to it (although it will not work as expected), you can't pass 0 as a second parameter (it expects int &). The compiler error however looks totally unrelated, whence the confusion.

    Kind regards.



  • @kshegunov
    Allright, thanks for the help.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.