×
Namespaces

Variants
Actions
(Difference between revisions)

How to launch other applications in Qt

From Nokia Developer Wiki
Jump to: navigation, search
croozeus (Talk | contribs)
m (Qt for S60 to Qt for Symbian)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(18 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[[Category:Qt]][[Category:Qt for Symbian]][[Category:How To]][[Category:Code Examples]]
+
[[Category:Qt]][[Category:How To]][[Category:Code Examples]]
{{CodeSnippet
+
{{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. }}
|id=...
+
 
|platform=S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
+
{{ArticleMetaData <!-- v1.2 -->
 +
|sourcecode= [[Media:QtAppLauncher.zip]]
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= Nokia 5800
 
|devices= Nokia 5800
|category=Qt for Symbian
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|subcategory=
+
|platform= Qt
|creationdate= 10 June 2009
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|keywords=QDesktopServices::openUrl(), QProcess::start()
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= Self-Signed
 +
|capabilities= None
 +
|keywords= QDesktopServices::openUrl(), QProcess::start()
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20090610
 +
|author= [[User:Savaj]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory=
 
}}
 
}}
 
 
==Overview==
 
 
Frequently we need to start other applications from our application. For example, need to launch browser to open web page or need to launch player to open video link. Also, sometime need to start our other application from current one. In Qt class QProcess is used to start external programs and to communicate with them.
 
 
The class QDesktopServices provides methods for accessing common desktop services, like browser, player, e-mail client, etc.<br>
 
 
QDesktopServices has a static method called 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/vedio.<br>
 
 
This snippet can be self-signed. As it does not use any API which require developer/certified signing.<br>
 
 
==Preconditions==
 
 
* [http://pepper.troll.no/s60prereleases/ Download the latest Qt for S60 distribution from Qt].
 
* Install Qt for S60:[[Installing Qt on S60]]<br>
 
* Check this link for installation guide: [http://pepper.troll.no/s60prereleases/doc/install-s60.html How to install the package].<br>
 
* Go through this article: [[Getting started with Qt for S60]]<br><br>
 
  
 
==Launching web browser==
 
==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'''
 
'''Header required'''
<code cpp>
+
<code cpp-qt>
 
#include <QDesktopServices>
 
#include <QDesktopServices>
 
#include <QUrl>
 
#include <QUrl>
Line 37: Line 37:
  
 
'''Source code'''
 
'''Source code'''
<code cpp>
+
<code cpp-qt>
 
//openUrl is static method of class QDesktopServices, so no need to create instance of it.
 
//openUrl is static method of class QDesktopServices, so no need to create instance of it.
 
//Open URL in browser
 
//Open URL in browser
QDesktopServices::openUrl(QUrl(tr("http://discussion.forum.nokia.com/forum/forumdisplay.php?f=40")));
+
QDesktopServices::openUrl(QUrl("http://www.developer.nokia.com/Community/Discussion/forumdisplay.php?40-General-Symbian-C"));
 
</code>  
 
</code>  
 
<br>
 
<br>
 
==Launching RealPlayer ==
 
==Launching RealPlayer ==
 +
QProcess is used to start external programs and to communicate with them.
 +
 +
  
 
'''Header required'''
 
'''Header required'''
<code cpp>
+
<code cpp-qt>
 
#include <QDesktopServices>
 
#include <QDesktopServices>
 
#include <QUrl>
 
#include <QUrl>
Line 52: Line 55:
  
 
'''Source code'''
 
'''Source code'''
<code cpp>
+
<code cpp-qt>
 
//openUrl is static method of class QDesktopServices, so no need to create instance of it.
 
//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.
 
//to open vedio file in realplayer give full path here.
QDesktopServices::openUrl(QUrl(tr("file:///e:/Videos/Nokia 5800 XpressMusic.mp4")));
+
QDesktopServices::openUrl(QUrl("file:///e:/Videos/Nokia 5800 XpressMusic.mp4"));
  
 
</code>  
 
</code>  
Line 62: Line 65:
  
 
'''Header required'''
 
'''Header required'''
<code cpp>
+
<code cpp-qt>
 
#include <QDesktopServices>
 
#include <QDesktopServices>
 
#include <QUrl>
 
#include <QUrl>
Line 68: Line 71:
  
 
'''Source code'''
 
'''Source code'''
<code cpp>
+
<code cpp-qt>
 
//openUrl is static method of class QDesktopServices, so no need to create instance of it.
 
//openUrl is static method of class QDesktopServices, so no need to create instance of it.
 
//Open email client.
 
//Open email client.
QDesktopServices::openUrl(QUrl(tr("mailto:ratang.gir@gmail.com?subject=Testing feedback&body=HelloFriend")));
+
QDesktopServices::openUrl(QUrl("mailto:ratang.gir@gmail.com?subject=Testing feedback&body=HelloFriend"));
 
</code>  
 
</code>  
 
<br>
 
<br>
Line 77: Line 80:
  
 
'''Header required'''
 
'''Header required'''
<code cpp>
+
<code cpp-qt>
 
#include <QProcess>
 
#include <QProcess>
 
</code>
 
</code>
Line 84: Line 87:
  
 
'''Header file'''
 
'''Header file'''
<code cpp>
+
<code cpp-qt>
 
void processError(QProcess::ProcessError err);
 
void processError(QProcess::ProcessError err);
  
Line 92: Line 95:
  
 
'''Source code'''
 
'''Source code'''
<code cpp>
+
<code cpp-qt>
 
QtAppLauncher::QtAppLauncher(QWidget *parent)
 
QtAppLauncher::QtAppLauncher(QWidget *parent)
 
     : QMainWindow(parent)
 
     : QMainWindow(parent)
Line 134: Line 137:
  
 
</code>  
 
</code>  
<br>
+
 
 
==Postconditions==
 
==Postconditions==
  
 
The code snippet is expected to launch browser, realplayer and helloworld application.
 
The code snippet is expected to launch browser, realplayer and helloworld application.
<br>
+
 
 
== Code Example ==
 
== Code Example ==
  
* Download the working [[Media:QtAppLauncher.zip|Code Example]].
+
* Download [[File:QtAppLauncher.zip]].[[Category:MeeGo Harmattan]] [[Category:Symbian]]
 +
<!-- Translation --> [[pt:Como executar o navegador Web, RealPlayer, e outras aplicações, em Qt]]

Latest revision as of 04:17, 11 October 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 ExampleTested with
Devices(s): Nokia 5800
Compatibility
Platform(s): Qt
Symbian
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
Keywords: QDesktopServices::openUrl(), QProcess::start()
Created: savaj (10 Jun 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

[edit] 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"));


[edit] 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"));


[edit] 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"));


[edit] 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;
}
}

[edit] Postconditions

The code snippet is expected to launch browser, realplayer and helloworld application.

[edit] Code Example

This page was last modified on 11 October 2012, at 04:17.
504 page views in the last 30 days.
×