QDesktopServices::openUrl() not working 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,对于此审查,您需要以下问题:如何在Qt Creator中启用C++11?,这表明您在.pro中添加了CONFIG += c++11
相关文章:
- Qt VTK交互风格的信号到小部件
- QT在错误的班级中寻找空位
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何在Qt C++中更改光标
- 如何在MS Visual Studio 2019中运行QT UI
- QT通过C++添加映射QML项目
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用Qt C++计算类似Git的SHA1哈希
- Qt focusObjectChanged example?
- 如何在cpp文件之间切换窗口?在Qt中
- Qt和C++:将QLineEdit添加到QTabWidget中
- QT qScriptConnect is not working
- QDesktopServices::openUrl() not working Qt
- (Qt C++) QPushButton setIcon() not working
- Qt C++ MainWindow::setMenuWidget not working
- Qt QString lastIndexOf not working
- Qt/Embedded: Caps Lock is not working