I wonder how to open a link in a default user browser using Qt (that would open it across all platforms (Win Mac Lin))?
2 Answers
In the doc: QDesktopServices
http://doc.qt.io/qt-4.8/qdesktopservices.html#openUrl
bool QDesktopServices::openUrl ( const QUrl & url ) [static]
Opens the given url in the appropriate Web browser for the user's desktop environment, and returns true if successful; otherwise returns false.
4 Comments
Donald Duck is with Ukraine
It doesn't work to put a string directly as an argument to that function. To open a URL from a string, you need to use the
QUrl() function like this: openUrl (QUrl("http://stackoverflow.com/"));jdi
Where does my answer suggest to use a string directly? You can see it says it needs a Url object to be passed.
Donald Duck is with Ukraine
I know. I just posted that comment to help people understand that. When I first saw it, I thought I had to use a string directly and it took some time before I understood that wasn't the case. I just wanted to help other people who would think the same thing.
Glinka
For some reasons it doesn't work if the link contains an anchor, e.g. doc.qt.io/qt-5/qwidget.html#QWidget
You can try this code
QString link = "http://www.google.com";
QDesktopServices::openUrl(QUrl(link));
Read QDesktopServices and QUrl to get further information.
3 Comments
Neurotransmitter
No, this is actually the single proper SO-style answer with the working example how
openUrl works. No need to go elsewhere and check specs.Pixxl
Don't forget to add
#include <QDesktopServices> to the top of your fileTimo Ernst
Think this is the better answer. Copy & paste – just works, thanks!