There is no cross-platform way of doing this. You could try QDS - Q Desktop Services, specifically the Launcher service.
Or something like the following code from the old forums:
bool open_browser
(QWidget* parent,
const QString
& rUrl
) {
bool result = false;
#ifdef Q_WS_WIN
result = int(ShellExecuteW(parent->winId(),
0,
rUrl.ucs2(),
0,
0,
SW_SHOWNORMAL)) > 32;
#else
Q_UNUSED(parent);
// Try a range of browsers available on UNIX, until we (hopefully)
// find one that works. Start with the most popular first.
bool process_started = false;
process.
setArguments(QStringList() <<
"netscape" << rUrl
);
process_started = process.start();
if (!process_started)
{
process.
setArguments(QStringList() <<
"mozilla" << rUrl
);
process_started = process.start();
}
if (!process_started)
{
process.
setArguments(QStringList() <<
"firefox" << rUrl
);
process_started = process.start();
}
if (!process_started)
{
process.
setArguments(QStringList() <<
"konqueror" << rUrl
);
process_started = process.start();
}
result = process_started;
#endif
return result;
}
bool open_browser(QWidget* parent, const QString& rUrl)
{
bool result = false;
QApplication::setOverrideCursor(Qt::BusyCursor);
#ifdef Q_WS_WIN
result = int(ShellExecuteW(parent->winId(),
0,
rUrl.ucs2(),
0,
0,
SW_SHOWNORMAL)) > 32;
#else
Q_UNUSED(parent);
// Try a range of browsers available on UNIX, until we (hopefully)
// find one that works. Start with the most popular first.
QProcess process;
bool process_started = false;
process.setArguments(QStringList() << "netscape" << rUrl);
process_started = process.start();
if (!process_started)
{
process.setArguments(QStringList() << "mozilla" << rUrl);
process_started = process.start();
}
if (!process_started)
{
process.setArguments(QStringList() << "firefox" << rUrl);
process_started = process.start();
}
if (!process_started)
{
process.setArguments(QStringList() << "konqueror" << rUrl);
process_started = process.start();
}
result = process_started;
#endif
QApplication::restoreOverrideCursor();
return result;
}
To copy to clipboard, switch view to plain text mode
Note that this is Qt3, so some changes may be required for Qt 4. Basically on Windows the code inside #ifdef Q_WS_WIN opens the default web browser. On Unix things are more complicated. Here, the code tries all kinds of different browsers until it succeeds.
Sometimes the user may have the $BROWSER variable setup that holds the "default" browser.
You can have an option for the user to set what browser they want to use. Many Linux apps do this, like QtAssistant (good source code here), etc... It has the benefit that it provides the user with control of setting that. For example, with the code above, your app will always open my mozilla browser (since it comes first), even though I may prefer firefox, or konqueror, which I also have. And there is no way for me to alter this behaviour.
Ofcourse, you can have an option and the above code. If the option isn't set, try above, and give up if you dont find a browser. If the user has a browser option set, open the prefered browser.
Under Linux, there really isn't such a thing as "the default browser".
Bojan
Bookmarks