使用 Qstring::toDouble 来检查数据

发布于 2024-12-24 17:27:04 字数 1121 浏览 3 评论 0原文

我正在尝试使用 QString::toDouble() 函数来验证用户输入。 文档说该函数应该像这样使用:

double QString::toDouble ( bool * ok = 0 ) const;
/* 
   Returns the string converted to a double value.
   Returns 0.0 if the conversion fails.
   If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
*/

因此,我尝试使用 *ok 来抛出错误消息(如果为 false),目的是只允许用户输入有效的整数或小数。问题是即使输入了单词,消息也始终返回有效。到目前为止,这是我的代码:

void MainWindow::checkData()
{        
    bool validate;
    QString tempStr;
    tempStr = ui->lineEditValidate->text();
    double converted = tempStr.toDouble(&validate);
    if (validate = false)
    {
        QErrorMessage validateError;
        validateError.showMessage("Input is Invalid");
        validateError.exec();
    }
    else
    {
        QErrorMessage worksFine;
        worksFine.showMessage("valid");
        worksFine.exec();
    }
}

我有一种感觉,我没有正确传递 validate 参数,但文档不够可靠,我无法真正了解;也许 QString::toDouble() 函数正在将字母转换为值。

有人可以解释我哪里出了问题吗?

I'm trying to validate user input by using the QString::toDouble() function. The documentation says the function should be used like this:

double QString::toDouble ( bool * ok = 0 ) const;
/* 
   Returns the string converted to a double value.
   Returns 0.0 if the conversion fails.
   If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
*/

So I was trying to use the *ok to throw an error message if its false with the objective of only allowing users to enter valid integers or decimals. The problem is the message always returns valid even when words are entered. Here is my code so far:

void MainWindow::checkData()
{        
    bool validate;
    QString tempStr;
    tempStr = ui->lineEditValidate->text();
    double converted = tempStr.toDouble(&validate);
    if (validate = false)
    {
        QErrorMessage validateError;
        validateError.showMessage("Input is Invalid");
        validateError.exec();
    }
    else
    {
        QErrorMessage worksFine;
        worksFine.showMessage("valid");
        worksFine.exec();
    }
}

I have a feeling that I am not passing the validate argument properly but the documentation isn't solid enough for me to really know; maybe the QString::toDouble() function is converting letters into values.

Could someone explain where I've gone wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔嚣张 2024-12-31 17:27:04
if (validate = false)
         //  ^ problem! this is an assignment

这样,您就可以系统地将 validate 设置为 false,并测试该分配的结果 - 这也是 false

这是不正确的。您需要:

if (validate == true) {
        //   ^^ comparison here
  // conversion worked
} else {
  // conversion failed
}

更常见的是省略布尔测试的比较:

if (valid) { // do stuff if valid ...

或者:

if (!valid) { // do stuff if not valid ...

您的变量最好命名为 validconversionOk 或类似的名称。它不是一个操作,也不指示某些内容是否需要验证,而是指示该操作/验证的结果。

if (validate = false)
         //  ^ problem! this is an assignment

With that, you're setting validate to false systematically, and testing the result of that assignment - which is false too.

This is incorrect. You need:

if (validate == true) {
        //   ^^ comparison here
  // conversion worked
} else {
  // conversion failed
}

It is even more usual to omit the comparison for boolean tests:

if (valid) { // do stuff if valid ...

Or:

if (!valid) { // do stuff if not valid ...

Your variable would be better named valid, or conversionOk or something like that. It's not an action, and it doesn't indicate whether something needs validation, but the result of that action/validation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文