这是我的代码:
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))));
图片正在显示,但它没有打开浏览器。请帮助我。
答案 0 :(得分:1)
你必须使用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