Qt 4.8
|
The QDesktopServices class provides methods for accessing common desktop services. More...
#include <qdesktopservices.h>
Public Types | |
enum | StandardLocation { DesktopLocation, DocumentsLocation, FontsLocation, ApplicationsLocation, MusicLocation, MoviesLocation, PicturesLocation, TempLocation, HomeLocation, DataLocation, CacheLocation } |
This enum describes the different locations that can be queried by QDesktopServices::storageLocation and QDesktopServices::displayName. More... | |
Static Public Functions | |
static QString | displayName (StandardLocation type) |
Returns a localized display name for the given location type or an empty QString if no relevant location can be found. More... | |
static bool | openUrl (const QUrl &url) |
Opens the given url in the appropriate Web browser for the user's desktop environment, and returns true if successful; otherwise returns false. More... | |
static void | setUrlHandler (const QString &scheme, QObject *receiver, const char *method) |
Sets the handler for the given scheme to be the handler method provided by the receiver object. More... | |
static QString | storageLocation (StandardLocation type) |
Returns the default system directory where files of type belong, or an empty string if the location cannot be determined. More... | |
static void | unsetUrlHandler (const QString &scheme) |
Removes a previously set URL handler for the specified scheme. More... | |
The QDesktopServices class provides methods for accessing common desktop services.
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.
The 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 QUrl argument.
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:
If inside the handler you decide that you can't open the requested URL, you can just call QDesktopServices::openUrl() again with the same argument, and it will try to open the URL using the appropriate mechanism for the user's desktop environment.
Definition at line 59 of file qdesktopservices.h.
This enum describes the different locations that can be queried by QDesktopServices::storageLocation and QDesktopServices::displayName.
Enumerator | |
---|---|
DesktopLocation | |
DocumentsLocation | |
FontsLocation | |
ApplicationsLocation | |
MusicLocation | |
MoviesLocation | |
PicturesLocation | |
TempLocation | |
HomeLocation | |
DataLocation | |
CacheLocation |
Definition at line 66 of file qdesktopservices.h.
|
static |
Returns a localized display name for the given location type or an empty QString if no relevant location can be found.
Definition at line 109 of file qdesktopservices_blackberry.cpp.
Referenced by getFullPath(), launchWebBrowser(), and openDocument().
|
static |
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.
The following example opens a file on the Windows file system residing on a path that contains spaces:
If a mailto
URL 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 mailto
links are handled by a Web browser.
For example, the following URL contains a recipient (user@ foo. com
), a subject (Test
), and a message body (Just a test
):
SwEvent
capability is required to open the given url if the Web browser is already running.Definition at line 190 of file qdesktopservices.cpp.
Referenced by QTextBrowserPrivate::_q_activateAnchor(), QTextControlPrivate::activateLinkUnderCursor(), and QDeclarativeEnginePrivate::desktopOpenUrl().
|
static |
Sets the handler for the given scheme to be the handler method provided by the receiver object.
This function provides a way to customize the behavior of openUrl(). If openUrl() is called with a URL with the specified scheme then the given method on the receiver object is called instead of QDesktopServices launching an external application.
The provided method must be implemented as a slot that only accepts a single QUrl argument.
If setUrlHandler() 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 QDesktopServices does 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 QDesktopServices::openUrl().
Definition at line 237 of file qdesktopservices.cpp.
|
static |
Returns the default system directory where files of type belong, or an empty string if the location cannot be determined.
Definition at line 70 of file qdesktopservices_blackberry.cpp.
Referenced by displayName(), getFullPath(), launchWebBrowser(), openDocument(), and QDeclarativeScriptEngine::QDeclarativeScriptEngine().
|
static |
Removes a previously set URL handler for the specified scheme.
Definition at line 258 of file qdesktopservices.cpp.