New in version 4.2.
Many desktop environments provide services that can be used by applications to perform common tasks, such as opening a web page, in a way that is both consistent and takes into account the user’s application preferences.
This class contains functions that provide simple interfaces to these services that indicate whether they succeeded or failed.
openUrl()function is used to open files located at arbitrary URLs in external applications. For URLs that correspond to resources on the local filing system (where the URL scheme is “file”), a suitable application will be used to open the file; otherwise, a web browser will be used to fetch and display the file.
The user’s desktop settings control whether certain executable file types are opened for browsing, or if they are executed instead. Some desktop environments are configured to prevent users from executing files obtained from non-local URLs, or to ask the user’s permission before doing so.
The behavior of the
openUrl()function can be customized for individual URL schemes to allow applications to override the default handling behavior for certain types of URLs.
The dispatch mechanism allows only one custom handler to be used for each URL scheme; this is set using the
setUrlHandler()function. Each handler is implemented as a slot which accepts only a single
The existing handlers for each scheme can be removed with the
unsetUrlHandler()function. This returns the handling behavior for the given scheme to the default behavior.
This system makes it easy to implement a help system, for example. Help could be provided in labels and text browsers using help://myapplication/mytopic URLs, and by registering a handler it becomes possible to display the help text inside the application:def showHelp(url): # ... pass QDesktopServices.setUrlHandler("help", showHelp);
If inside the handler you decide that you can’t open the requested URL, you can just call
openUrl()again with the same argument, and it will try to open the URL using the appropriate mechanism for the user’s desktop environment.
Combined with platform specific settings, the schemes registered by the
openUrl()function can also be exposed to other applications, opening up for application deep linking or a very basic URL-based IPC mechanism.
Since Qt 5,
displayName()are replaced by functionality provided by the
- Return type
Opens the given
urlin the appropriate Web browser for the user’s desktop environment, and returns
trueif successful; otherwise returns
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.
The following example opens a file on the Windows file system residing on a path that contains spaces:
QDesktopServices.openUrl(QUrl("file:///C:/Documents and Settings/All Users/Desktop", QUrl.TolerantMode))
mailtoURL is specified, the user’s e-mail client will be used to open a composer window containing the options specified in the URL, similar to the way
mailtolinks are handled by a Web browser.
For example, the following URL contains a recipient (
firstname.lastname@example.org), a subject (
Test), and a message body (
Just a test):
mailto:email@example.com?subject=Test&body=Just a test
Although many e-mail clients can send attachments and are Unicode-aware, the user may have configured their client without these features. Also, certain e-mail clients (e.g., Lotus Notes) have problems with long URLs.
A return value of
trueindicates that the application has successfully requested the operating system to open the URL in an external application. The external application may still fail to launch or fail to open the requested URL. This result will not be reported back to the application.
URLs passed to this function on iOS will not load unless their schemes are listed in the
LSApplicationQueriesSchemeskey of the application’s Info.plist file. For more information, see the Apple Developer Documentation for canOpenURL(_:) . For example, the following lines enable URLs with the HTTPS scheme:
<key>LSApplicationQueriesSchemes</key> <array> <string>https</string> </array>
setUrlHandler(scheme, receiver, method)¶
scheme – unicode
method – str
Sets the handler for the given
schemeto be the handler
methodprovided by the
This function provides a way to customize the behavior of
openUrl()is called with a URL with the specified
schemethen the given
receiverobject is called instead of
QDesktopServiceslaunching an external application.
The provided method must be implemented as a slot that only accepts a single
def showHelp(url): # ... pass QDesktopServices.setUrlHandler("help", showHelp);
To use this function for receiving data from other apps on iOS you also need to add the custom scheme to the
CFBundleURLSchemeslist in your Info.plist file:
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>myapp</string> </array> </dict> </array>
For more information, see the Apple Developer Documentation for Communicating with Other Apps Using Custom URLs .
If is used to set a new handler for a scheme which already has a handler, the existing handler is simply replaced with the new one. Since
QDesktopServicesdoes not take ownership of handlers, no objects are deleted when a handler is replaced.
Note that the handler will always be called from within the same thread that calls
© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.