QDesktopServices::openUrl() 不工作 Qt

QDesktopServices::openUrl() not working Qt

这是我的代码:

QPixmap map(":/Medal.jpg");
QIcon ico(map);
ico.addPixmap(map);
QPushButton *p = new QPushButton;
p->setIcon(ico);
QString link = "http://www.google.com";
QObject::connect(p, SIGNAL(clicked()),window,SLOT(QDesktopServices::openUrl(QUrl (link))));

图片正在显示,但未打开浏览器。请帮助我。

您必须使用 lambda 函数:

#include <QApplication>
#include <QDesktopServices>
#include <QPushButton>
#include <QUrl>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QPushButton p("Click me");
    QString link = "http://www.google.com";
    QObject::connect(&p, &QPushButton::clicked, [&link](){
        QDesktopServices::openUrl(QUrl(link));
    });
    p.show();

    return a.exec();
}

std::bind()

#include <QApplication>
#include <QDesktopServices>
#include <QPushButton>
#include <QUrl>
#include <functional>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QPushButton p("Click me");
    QString link = "http://www.google.com";
    QObject::connect(&p, &QPushButton::clicked, std::bind(QDesktopServices::openUrl, QUrl(link)));
    p.show();

    return a.exec();
}

注:

您需要在 Qt 中启用 C++11,为此请查看以下问题:How to enable C++11 in Qt Creator?,这表明您在 .pro

中添加 CONFIG += c++11