Qt 检查有效的 URL [英] Qt check for valid URL

查看:88
本文介绍了Qt 检查有效的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 Qt 应用程序,用于检查用户在文本编辑中输入的 URL 是否有效.

I am trying to create a Qt application which checks if a URL entered by the user into a text edit is valid.

这是我到目前为止所拥有的,但它只说输入的 URL 有效,即使我输入的 URL 无效.

This is what I have so far but it only ever says the URL entered is valid, even when I enter one which is not.

bool checkUrl(const QUrl &url) {
    if (!url.isValid()) {
        //qDebug(QString("Invalid URL: %1").arg(url.toString()));
        return false;
    }
    return true;
}

void MainWindow::on_pushButton_clicked()
{
    QString usertext = ui->plainTextEdit->toPlainText();
    QUrl url = QUrl::fromUserInput(usertext);
    if (checkUrl(url))
        ui->textEdit->setPlainText("Valid URL.");
    else
        ui->textEdit->setPlainText("Invalid URL.");
}

同样在 qDebug 行上有一个错误:

Also on the qDebug line there is an error:

/home/user/HTML/mainwindow.cpp:32: error: no matching function for call to ‘qDebug(QString)’

有没有人知道问题是什么,因为它一直返回 true?

Does anyone know what the problem is as it keeps returning true?

推荐答案

你应该像这样使用 qDebug:

qDebug() << QString("Invalid URL: %1").arg(url.toString());

还要注意 QUrl::isValid() 不检查 url 的语法.您可能希望使用正则表达式来验证网址.

also note that QUrl::isValid() does not check syntax of url. You may want to use regular expressions to validate urls.

这篇关于Qt 检查有效的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆