×
Namespaces

Variants
Actions
(Difference between revisions)

How to launch other applications in Qt

From Nokia Developer Wiki
Jump to: navigation, search
kiran10182 (Talk | contribs)
divanov (Talk | contribs)
Line 39: Line 39:
 
//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://discussion.forum.nokia.com/forum/forumdisplay.php?f=40"));
 
</code>  
 
</code>  
 
<br>
 
<br>
Line 54: Line 54:
 
//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 70: Line 70:
 
//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>

Revision as of 23:51, 6 April 2011

{{{width}}}
Article Metadata
Tested with
Devices(s): Nokia 5800
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: QDesktopServices::openUrl(), QProcess::start()
Created: (10 Jun 2009)
Last edited: divanov (06 Apr 2011)


Contents

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.

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/video.

This snippet can be self-signed. As it does not use any API which require developer/certified signing.

Preconditions

Launching web browser

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://discussion.forum.nokia.com/forum/forumdisplay.php?f=40"));


Launching RealPlayer

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

488 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×