QDesktopServices 類

The QDesktopServices class provides methods for accessing common desktop services. 更多...

頭: #include <QDesktopServices>
Since: Qt 4.2

公共類型

enum StandardLocation { DesktopLocation, DocumentsLocation, FontsLocation, ApplicationsLocation, ..., CacheLocation }

靜態公共成員

QString displayName (StandardLocation type )
bool openUrl (const QUrl & url )
void setUrlHandler (const QString & scheme , QObject * receiver , const char * method )
QString storageLocation (StandardLocation type )
void unsetUrlHandler (const QString & scheme )

詳細描述

The QDesktopServices class provides methods for accessing common desktop services.

許多桌麵環境提供可用於應用程序以履行常見任務的服務 (譬如:打開網頁),按一緻且考慮到用戶應用程序首選的方式。

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.

URL 處理程序

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 自變量。

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:

class MyHelpHandler : public QObject
{
    Q_OBJECT
public:
    ...
public slots:
    void showHelp(const QUrl &url);
};
QDesktopServices::setUrlHandler("help", helpInstance, "showHelp");
					

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.

另請參閱 QSystemTrayIcon and QProcess .

成員類型文檔編製

enum QDesktopServices:: StandardLocation

This enum describes the different locations that can be queried by QDesktopServices::storageLocation and QDesktopServices::displayName .

常量 描述
QDesktopServices::DesktopLocation 0 Returns the user's desktop directory.
QDesktopServices::DocumentsLocation 1 Returns the user's document.
QDesktopServices::FontsLocation 2 Returns the user's fonts.
QDesktopServices::ApplicationsLocation 3 Returns the user's applications.
QDesktopServices::MusicLocation 4 Returns the users music.
QDesktopServices::MoviesLocation 5 Returns the user's movies.
QDesktopServices::PicturesLocation 6 Returns the user's pictures.
QDesktopServices::TempLocation 7 Returns the system's temporary directory.
QDesktopServices::HomeLocation 8 Returns the user's home directory.
QDesktopServices::DataLocation 9 Returns a directory location where persistent application data can be stored. QCoreApplication::applicationName and QCoreApplication::organizationName should work on all platforms.
QDesktopServices::CacheLocation 10 Returns a directory location where user-specific non-essential (cached) data should be written.

該枚舉在 Qt 4.4 引入或被修改。

另請參閱 storageLocation () 和 displayName ().

成員函數文檔編製

[static] QString QDesktopServices:: displayName ( StandardLocation type )

返迴本地化顯示名為給定位置 type 或空 QString 若找不到相關位置。

[static] bool QDesktopServices:: 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.

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));
					

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 ( 測試 ), and a message body ( Just a test ):

mailto:user@foo.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.

注意: On Symbian OS, SwEvent capability is required to open the given url if the Web browser is already running.

另請參閱 setUrlHandler ().

[static] void QDesktopServices:: setUrlHandler (const QString & scheme , QObject * receiver , const char * method )

設置處理程序為給定 scheme to be the handler method provided by the receiver 對象。

This function provides a way to customize the behavior of openUrl ()。若 openUrl () is called with a URL with the specified scheme then the given method receiver object is called instead of QDesktopServices 發起外部應用程序。

提供方法必須被實現成槽,且其隻接受單 QUrl 自變量。

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 不擁有處理程序的所有權,因此,當替換處理程序時沒有對象會被刪除。

注意:總是在其內調用處理程序的綫程,也會調用 QDesktopServices::openUrl ().

另請參閱 openUrl () 和 unsetUrlHandler ().

[static] QString QDesktopServices:: storageLocation ( StandardLocation type )

Returns the default system directory where files of type belong, or an empty string if the location cannot be determined.

注意: The storage location returned can be a directory that does not exist; i.e., it may need to be created by the system or the user.

注意: On Symbian OS, ApplicationsLocation always point /sys/bin folder on the same drive with executable. FontsLocation always points to folder on ROM drive. Symbian OS does not have desktop concept, DesktopLocation returns same path as DocumentsLocation . Rest of the standard locations point to folder on same drive with executable, except that if executable is in ROM the folder from C drive is returned.

該函數在 Qt 4.4 引入。

[static] void QDesktopServices:: unsetUrlHandler (const QString & scheme )

移除先前設置的 URL 處理程序,為指定的 scheme .

另請參閱 setUrlHandler ().