3

Is there a Qt interface to get the system's default browser?

I want to open a file:// url explicitly in a browser instead of the system's default application, so QDesktopServices::openUrl is not the way because of

If the URL is a reference to a local file (i.e., the URL scheme is "file") then it will be opened with a suitable application instead of a Web browser.

folibis
  • 12,048
  • 6
  • 54
  • 97
Simon Warta
  • 10,850
  • 5
  • 40
  • 78
  • 1
    You should not really outsmart the system settings. Why are you trying to force your hand there? What kind of files are you talking about? – peppe Aug 15 '16 at 12:15
  • a license file (.html) which should not be opened in any kind of IDE or text editor but be rendered. – Simon Warta Aug 15 '16 at 12:18
  • 1
    Well, if the user decides to set a IDE to open `html` files then it's her problem, I'd say... Is that HTML content under your control? You could make it "simple enough" to be rendered by QTextBrowser. (Or go all-in and ship WebKit/Chromium with your app). – peppe Aug 15 '16 at 13:33
  • Yes, I generate and ship the .html. I could change it to a PDF as well if necessary. The point is, that the user never gets the file in his hands. So this is not a "user opens file with preferred application" action but a "show licenses" action. Thus the file format is a implementation detail irrelevant to the user. QTextBrowser is an option, yes. Other file formats as well. Local HTTP server too. But this does not answer the question. I can't believe it's hard to open something in a browser. – Simon Warta Aug 15 '16 at 13:51
  • Because it's hardly a requirement. If you want, just deal with a tiny bit of platform specific code. Under FDG-complaint desktops, you can invoke `x-www-browser`; I have no idea about Windows or macOS. – peppe Aug 15 '16 at 15:31

1 Answers1

0

For your particular application, you should use the web engine that comes with Qt:

QWebEngineView *view = new QWebEngineView{parent};
view->load(QUrl{"file://...."});
view->setWindowFlags(Qt::Window);
view->setAttribute(Qt::WA_DeleteOnClose);
view->show();

If the html is simple enough, use the text browser:

QTextBrowser *view = new QTextBrowser{parent};
view->setSource(Qurl{"file://...."});
view->setWindowFlags(Qt::Window);
view->setAttribute(Qt::WA_DeleteOnClose);
view->show();
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313