How to launch other applications in Qt
hamishwillee
(Talk | contribs) m (Hamishwillee - Bot update - Fix ArticleMetaData and RevieweApproval) |
hamishwillee
(Talk | contribs) m (Hamishwillee - Tidy wiki text) |
||
| Line 1: | Line 1: | ||
| − | [[Category:Qt]] | + | [[Category:Qt]][[Category:How To]][[Category:Code Examples]] |
| − | + | {{Abstract|This code example shows how to start other applications from within a Qt application. It demonstrates [http://doc.qt.nokia.com/stable/qdesktopservices.html#openUrl QDesktopServices::openUrl()] for opening URLs in the default browser and [http://doc.qt.nokia.com/stable/qprocess.html QProcess] for launching arbitrary applications. }} | |
{{ArticleMetaData <!-- v1.1 --> | {{ArticleMetaData <!-- v1.1 --> | ||
| Line 10: | Line 10: | ||
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) --> | |devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) --> | ||
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> | |dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> | ||
| − | |signing= | + | |signing= Self-Signed |
| − | |capabilities= | + | |capabilities= None |
|keywords= QDesktopServices::openUrl(), QProcess::start() | |keywords= QDesktopServices::openUrl(), QProcess::start() | ||
| − | |||
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese --> | |language= <!-- Language category code for non-English topics - e.g. Lang-Chinese --> | ||
|translated-by= <!-- [[User:XXXX]] --> | |translated-by= <!-- [[User:XXXX]] --> | ||
| Line 28: | Line 27: | ||
|category= Qt | |category= Qt | ||
}} | }} | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
==Launching web browser== | ==Launching web browser== | ||
| Line 158: | Line 146: | ||
* Download [[File:QtAppLauncher.zip]]. | * Download [[File:QtAppLauncher.zip]]. | ||
| − | |||
| − | |||
Revision as of 07:23, 10 February 2012
This code example shows how to start other applications from within a Qt application. It demonstrates QDesktopServices::openUrl() for opening URLs in the default browser and QProcess for launching arbitrary applications.
Article Metadata
Code Example
Tested with
Compatibility
Platform Security
Article
Contents |
Launching web browser
The class QDesktopServices provides methods for accessing common desktop services, like browser, player, e-mail client, etc. We use QDesktopServices::openUrl(), which opens the given URL in the appropriate Web browser for the user's desktop environment, and returns true if successful; otherwise returns false. 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. For example, you can use URL "file:///c:/data/abc.png" to open abc.png in its default application. similarly you can play audio/video.
Header required
#include <QDesktopServices>
#include <QUrl>
Source code
//openUrl is static method of class QDesktopServices, so no need to create instance of it.
//Open URL in browser
QDesktopServices::openUrl(QUrl("http://www.developer.nokia.com/Community/Discussion/forumdisplay.php?40-General-Symbian-C"));
Launching RealPlayer
QProcess is used to start external programs and to communicate with them.
Header required
#include <QDesktopServices>
#include <QUrl>
Source code
//openUrl is static method of class QDesktopServices, so no need to create instance of it.
//to open vedio file in realplayer give full path here.
QDesktopServices::openUrl(QUrl("file:///e:/Videos/Nokia 5800 XpressMusic.mp4"));
Launching Email Client
Header required
#include <QDesktopServices>
#include <QUrl>
Source code
//openUrl is static method of class QDesktopServices, so no need to create instance of it.
//Open email client.
QDesktopServices::openUrl(QUrl("mailto:ratang.gir@gmail.com?subject=Testing feedback&body=HelloFriend"));
Launching other applications
Header required
#include <QProcess>The signal error() of QProcess is emitted when an error occurs with the process. The specified error describes the type of error that occurred.
Header file
void processError(QProcess::ProcessError err);
QProcess process;
Source code
QtAppLauncher::QtAppLauncher(QWidget *parent)
: QMainWindow(parent)
{
//connect error() to slot processError() to get error, if occurs.
QObject::connect( &process, SIGNAL(error(QProcess::ProcessError)),
this, SLOT(processError(QProcess::ProcessError)));
//pass the name of executable that you want to launch. i have installed helloworld so passed helloworld.
process.start("helloworld");
}
//get errors, if any
void QtAppLauncher::processError(QProcess::ProcessError err)
{
switch(err)
{
case QProcess::FailedToStart:
QMessageBox::information(0,"FailedToStart","FailedToStart");
break;
case QProcess::Crashed:
QMessageBox::information(0,"Crashed","Crashed");
break;
case QProcess::Timedout:
QMessageBox::information(0,"FailedToStart","FailedToStart");
break;
case QProcess::WriteError:
QMessageBox::information(0,"Timedout","Timedout");
break;
case QProcess::ReadError:
QMessageBox::information(0,"ReadError","ReadError");
break;
case QProcess::UnknownError:
QMessageBox::information(0,"UnknownError","UnknownError");
break;
default:
QMessageBox::information(0,"default","default");
break;
}
}
Postconditions
The code snippet is expected to launch browser, realplayer and helloworld application.
Code Example
- Download File:QtAppLauncher.zip.

