77

I wonder how to open a link in a default user browser using Qt (that would open it across all platforms (Win Mac Lin))?

0

2 Answers 2

92

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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/"));
Where does my answer suggest to use a string directly? You can see it says it needs a Url object to be passed.
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.
For some reasons it doesn't work if the link contains an anchor, e.g. doc.qt.io/qt-5/qwidget.html#QWidget
40

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

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.
Don't forget to add #include <QDesktopServices> to the top of your file
Think this is the better answer. Copy & paste – just works, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.