QCoreApplication 類

The QCoreApplication class provides an event loop for console Qt applications. 更多...

頭: #include <QCoreApplication>
繼承: QObject
繼承者: QApplication

公共類型

enum 編碼 { CodecForTr, UnicodeUTF8, DefaultCodec }
typedef EventFilter

特性

公共函數

QCoreApplication (int & argc , char ** argv )
~QCoreApplication ()
bool filterEvent (void * message , long * result )
virtual bool notify (QObject * receiver , QEvent * event )
EventFilter setEventFilter (EventFilter filter )
virtual bool winEventFilter (MSG * msg , long * result )

公共槽

void quit ()

信號

void aboutToQuit ()

靜態公共成員

void addLibraryPath (const QString & path )
QString applicationDirPath ()
QString applicationFilePath ()
QString applicationName ()
qint64 applicationPid ()
QString applicationVersion ()
QStringList arguments ()
bool closingDown ()
int exec ()
void exit (int returnCode = 0)
void flush ()
bool hasPendingEvents ()
void installTranslator (QTranslator * translationFile )
QCoreApplication * instance ()
QStringList libraryPaths ()
QString organizationDomain ()
QString organizationName ()
void postEvent (QObject * receiver , QEvent * event )
void postEvent (QObject * receiver , QEvent * event , int priority )
void processEvents (QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents)
void processEvents (QEventLoop::ProcessEventsFlags flags , int maxtime )
void removeLibraryPath (const QString & path )
void removePostedEvents (QObject * receiver )
void removePostedEvents (QObject * receiver , int eventType )
void removeTranslator (QTranslator * translationFile )
bool sendEvent (QObject * receiver , QEvent * event )
void sendPostedEvents (QObject * receiver , int event_type )
void sendPostedEvents ()
void setApplicationName (const QString & application )
void setApplicationVersion (const QString & version )
void setAttribute (Qt::ApplicationAttribute attribute , bool on = true)
void setLibraryPaths (const QStringList & paths )
void setOrganizationDomain (const QString & orgDomain )
void setOrganizationName (const QString & orgName )
bool startingUp ()
bool testAttribute (Qt::ApplicationAttribute attribute )
QString translate (const char * context , const char * sourceText , const char * disambiguation , Encoding encoding , int n )
QString translate (const char * context , const char * sourceText , const char * disambiguation = 0, Encoding encoding = CodecForTr)

重實現保護函數

virtual bool event (QEvent * e )
void qAddPostRoutine (QtCleanUpFunction ptr )

Q_DECLARE_TR_FUNCTIONS ( context )

詳細描述

The QCoreApplication class provides an event loop for console Qt applications.

非 GUI 應用程序使用此類來提供事件循環。對於使用 Qt 的非 GUI 應用程序,應準確存在一個 QCoreApplication 對象。對於 GUI 應用程序,見 QApplication .

QCoreApplication 包含主事件循環,會處理和分派來自操作係統 (如:計時器和網絡事件) 和其它源的所有事件。它還處理應用程序的初始化和定稿,及係統範圍和應用程序範圍的設置。

事件循環和事件處理

開始事件循環通過調用 exec (). Long running operations can call processEvents () 以保持應用程序的響應速度。

一般而言,推薦創建 QCoreApplication QApplication 對象在 main() 函數要盡可能早。 exec () 不會返迴直到事件循環退齣;如:當 quit () 被調用。

還提供瞭幾個靜態方便函數。 QCoreApplication 對象可獲取自 instance (). Events can be sent or posted using sendEvent (), postEvent (),和 sendPostedEvents ()。待決事件可以移除采用 removePostedEvents () or flushed with flush ().

類提供 quit () 槽和 aboutToQuit () 信號。

應用程序和庫路徑

應用程序有 applicationDirPath () 和 applicationFilePath ()。庫路徑 (見 QLibrary ) 可以被檢索采用 libraryPaths () 和操縱通過 setLibraryPaths (), addLibraryPath (),和 removeLibraryPath ().

國際化和翻譯

添加 (或移除) 翻譯文件可以使用 installTranslator () 和 removeTranslator ()。可以翻譯應用程序字符串使用 translate ()。 QObject::tr () 和 QObject::trUtf8 () functions are implemented in terms of translate ().

訪問命令行自變量

命令行自變量被傳遞給 QCoreApplication 構造函數的訪問應該是使用 arguments () function. Note that some arguments supplied by the user may have been processed and removed by QCoreApplication .

In cases where command line arguments need to be obtained using the argv() function, you must convert them from the local string encoding using QString::fromLocal8Bit ().

區域設置

在 Unix/Linux,Qt 默認配置為使用係統區域設置。這會導緻衝突當使用 POSIX 函數時,例如,當在數據類型 (譬如:浮點數和字符串) 之間轉換時,由於錶示法可能因區域設置不同而異。要解決此問題,調用 POSIX 函數 setlocale(LC_NUMERIC,"C") 恰好先於初始化 QApplication or QCoreApplication 以將用於數字格式化的區域設置重置為 C 區域設置。

另請參閱 QApplication , QAbstractEventDispatcher , QEventLoop , 信號量範例 ,和 等待條件範例 .

成員類型文檔編製

enum QCoreApplication:: 編碼

This enum type defines the 8-bit encoding of character string arguments to translate ():

常量 描述
QCoreApplication::CodecForTr 0 The encoding specified by QTextCodec::codecForTr () (Latin-1 if none has been set).
QCoreApplication::UnicodeUTF8 1 UTF-8.
QCoreApplication::DefaultCodec CodecForTr (Obsolete) Use CodecForTr instead.

另請參閱 QObject::tr (), QObject::trUtf8 (),和 QString::fromUtf8 ().

typedef QCoreApplication:: EventFilter

A function with the following signature that can be used as an event filter:

bool myEventFilter(void *message, long *result);
					

另請參閱 setEventFilter ().

特性文檔編製

applicationName : QString

This property holds the name of this application.

值的使用是通過 QSettings 類當使用空構造函數構造它時。這節省必須重復此信息每次 QSettings 對象被創建。

On BlackBerry this property is read-only. It is obtained from the BAR application descriptor file.

訪問函數:

QString applicationName ()
void setApplicationName (const QString & application )

另請參閱 organizationName , organizationDomain ,和 applicationVersion .

applicationVersion : QString

This property holds the version of this application.

On BlackBerry this property is read-only. It is obtained from the BAR application descriptor file.

該特性在 Qt 4.4 引入。

訪問函數:

QString applicationVersion ()
void setApplicationVersion (const QString & version )

另請參閱 applicationName , organizationName ,和 organizationDomain .

organizationDomain : QString

This property holds the Internet domain of the organization that wrote this application.

值的使用是通過 QSettings 類當使用空構造函數構造它時。這節省必須重復此信息每次 QSettings 對象被創建。

在 Mac, QSettings 使用 organizationDomain() 作為組織若它不是空字符串;否則它使用 organizationName ()。在所有其它平颱, QSettings 使用 organizationName () 作為組織。

訪問函數:

QString organizationDomain ()
void setOrganizationDomain (const QString & orgDomain )

另請參閱 organizationName , applicationName ,和 applicationVersion .

organizationName : QString

This property holds the name of the organization that wrote this application.

值的使用是通過 QSettings 類當使用空構造函數構造它時。這節省必須重復此信息每次 QSettings 對象被創建。

在 Mac, QSettings 使用 organizationDomain () 作為組織若它不是空字符串;否則它使用 organizationName()。在所有其它平颱, QSettings 使用 organizationName() 作為組織。

On BlackBerry this property is read-only. It is obtained from the BAR application descriptor file.

訪問函數:

QString organizationName ()
void setOrganizationName (const QString & orgName )

另請參閱 organizationDomain and applicationName .

成員函數文檔編製

QCoreApplication:: QCoreApplication ( int & argc , char ** argv )

Constructs a Qt kernel application. Kernel applications are applications without a graphical user interface. These type of applications are used at the console or as server processes.

The argc and argv 自變量由應用程序處理,並以更方便形式使之可用通過 arguments () 函數。

警告: 數據引用通過 argc and argv 必須保持有效對於整個生命周期 QCoreApplication 對象。此外, argc 必須大於 0 且 argv 必須包含至少一條有效字符串。

QCoreApplication:: ~QCoreApplication ()

銷毀 QCoreApplication 對象。

[signal] void QCoreApplication:: aboutToQuit ()

此信號發射,當應用程序即將離開主事件循環 (如:當事件循環級彆降至 0 時)。這可能發生,若之前有調用 quit () from inside the application or when the users shuts down the entire desktop session.

信號特彆有用,若應用程序必須在最後一秒做一些清理。注意,在此狀態下不可能進行用戶交互。

另請參閱 quit ().

[static] void QCoreApplication:: addLibraryPath (const QString & path )

前置 path 到庫路徑列錶的開頭,確保庫被首先搜索。若 path 為空或已在路徑列錶中,路徑列錶不改變。

默認路徑列錶由單個條目、插件安裝目錄組成。插件的默認安裝目錄為 INSTALL/plugins ,其中 INSTALL 是 Qt 安裝目錄。

In Symbian this function is only useful for adding paths for finding Qt extension plugin stubs, since the OS can only load libraries from the /sys/bin 目錄。

另請參閱 removeLibraryPath (), libraryPaths (),和 setLibraryPaths ().

[static] QString QCoreApplication:: applicationDirPath ()

返迴包含應用程序可執行文件的目錄。

例如,若有安裝 Qt 在 C:\Trolltech\Qt 目錄,和運行 regexp example, this function will return "C:/Trolltech/Qt/examples/tools/regexp".

On Mac OS X this will point to the directory actually containing the executable, which may be inside of an application bundle (if the application is bundled).

警告: 在 Linux,此函數將試著獲取路徑從 /proc 文件係統。若失敗,假定 argv[0] 包含可執行文件的絕對文件名。函數還假定當前目錄未被應用程序所改變。

In Symbian this function will return the application private directory, not the path to executable itself, as those are always in /sys/bin . If the application is in a read only drive, i.e. ROM, then the private path on the system drive will be returned.

另請參閱 applicationFilePath ().

[static] QString QCoreApplication:: applicationFilePath ()

返迴應用程序可執行文件的文件路徑。

例如,若有安裝 Qt 在 /usr/local/qt 目錄,和運行 regexp 範例,此函數將返迴 /usr/local/qt/examples/tools/regexp/regexp。

警告: 在 Linux,此函數將試著獲取路徑從 /proc 文件係統。若失敗,假定 argv[0] 包含可執行文件的絕對文件名。函數還假定當前目錄未被應用程序所改變。

另請參閱 applicationDirPath ().

[static] qint64 QCoreApplication:: applicationPid ()

返迴應用程序的當前進程 ID。

該函數在 Qt 4.4 引入。

[static] QStringList QCoreApplication:: arguments ()

返迴命令行自變量列錶。

通常 arguments().at(0) 是程序名,arguments().at(1) 是第一自變量,和 arguments().last() 是最後一個自變量。見下文有關 Windows 的注意事項。

調用此函數很慢 - 應將結果存儲在變量中,當剖析命令行時。

警告: 在 Unix,此列錶構建自傳遞給 main() 函數構造函數的 argc 和 argv 參數。argv 中字符串數據的解釋是使用 QString::fromLocal8Bit ();因此,例如,在按 Latin1 區域設置運行的係統中,傳遞日語命令行自變量是不可能的。大多數現代 Unix 係統沒有此局限性,因為它們基於 Unicode。

On NT-based Windows, this limitation does not apply either. On Windows, the arguments() are not built from the contents of argv/argc, as the content does not support Unicode. Instead, the arguments() are constructed from the return value of GetCommandLine() 。因此,由 arguments().at(0) 給齣的字符串可能不是 Windows 程序名,從屬應用程序如何啓動。

For Symbian applications started with RApaLsSession::StartApp one can specify arguments using CApaCommandLine::SetTailEndL function. Such arguments are only available via this method; they will not be passed to main function. Also note that only 8-bit string data set with CApaCommandLine::SetTailEndL is supported by this function.

該函數在 Qt 4.1 引入。

另請參閱 applicationFilePath ().

[static] bool QCoreApplication:: closingDown ()

Returns true if the application objects are being destroyed; otherwise returns false.

另請參閱 startingUp ().

[virtual protected] bool QCoreApplication:: event ( QEvent * e )

重實現自 QObject::event ().

[static] int QCoreApplication:: exec ()

進入主事件循環並等待,直到 exit () is called. Returns the value that was set to exit () (為 0 若 exit () 被調用憑藉 quit ()).

有必要調用此函數以啓動事件處理。主事件循環從窗口係統接收事件,並將其分派給應用程序 Widget。

要使應用程序履行空閑處理 (即:執行特殊函數每當沒有待決事件時),使用 QTimer 采用 0 超時。更高級空閑處理方案可以達成使用 processEvents ().

推薦把清理代碼連接到 aboutToQuit() 信號,而不是將它放入應用程序的 main() function because on some platforms the QCoreApplication::exec() call may not return. For example, on Windows when the user logs off, the system terminates the process after Qt closes all top-level windows. Hence, there is no guarantee that the application will have time to exit its event loop and execute code at the end of the main() function after the QCoreApplication::exec() call.

另請參閱 quit (), exit (), processEvents (),和 QApplication::exec ().

[static] void QCoreApplication:: exit ( int returnCode = 0)

告訴應用程序采用返迴代碼去退齣。

在調用此函數後,應用程序離開主事件循環並返迴從調用的 exec ()。 exec () 函數返迴 returnCode 。若事件循環未運行,此函數什麼都不做。

按約定, returnCode 0 意味著成功,而任何非零值指示齣錯。

注意:不像同名 C 庫函數,此函數 does 返迴給調用者 -- 它停止事件處理。

另請參閱 quit () 和 exec ().

bool QCoreApplication:: filterEvent ( void * message , long * result )

發送 message through the event filter that was set by setEventFilter (). If no event filter has been set, this function returns false; otherwise, this function returns the result of the event filter function in the result 參數。

另請參閱 setEventFilter ().

[static] void QCoreApplication:: flush ()

Flushes the platform specific event queues.

If you are doing graphical changes inside a loop that does not return to the event loop on asynchronous window systems like X11 or double buffered window systems like Mac OS X, and you want to visualize these changes immediately (e.g. Splash Screens), call this function.

另請參閱 sendPostedEvents ().

[static] bool QCoreApplication:: hasPendingEvents ()

This function returns true if there are pending events; otherwise returns false. Pending events can be either from the window system or posted events using postEvent ().

另請參閱 QAbstractEventDispatcher::hasPendingEvents ().

[static] void QCoreApplication:: installTranslator ( QTranslator * translationFile )

添加翻譯文件 translationFile 到用於翻譯的翻譯文件列錶。

可以安裝多個翻譯文件。按照安裝它們時的反嚮次序搜索翻譯,因此,首先搜索最近安裝的翻譯文件,最後搜索首先安裝的翻譯文件。一旦發現包含匹配字符串的翻譯,就停止搜索。

安裝或移除 QTranslator ,或改變安裝的 QTranslator 生成 LanguageChange 事件為 QCoreApplication 實例。 QApplication instance will propagate the event to all toplevel windows, where a reimplementation of changeEvent can re-translate the user interface by passing user-visible strings via the tr () function to the respective property setters. User-interface classes generated by Qt Designer provide a retranslateUi() 函數可以被調用。

另請參閱 removeTranslator (), translate (), QTranslator::load (),和 動態翻譯 .

[static] QCoreApplication * QCoreApplication:: instance ()

返迴指針指嚮應用程序的 QCoreApplication (或 QApplication ) 實例。

若實例尚未分配, null 被返迴。

[static] QStringList QCoreApplication:: libraryPaths ()

返迴將搜索的應用程序路徑列錶當動態加載庫時。

Qt 提供默認庫路徑,但也可以設置它們使用 qt.conf file. Paths specified in this file will override default values.

This list will include the installation directory for plugins if it exists (the default installation directory for plugins is INSTALL/plugins ,其中 INSTALL is the directory where Qt was installed). The directory of the application executable (NOT the working directory) is always added, as well as the colon separated entries of the QT_PLUGIN_PATH environment variable.

若想要遍曆列錶,可以使用 foreach 僞關鍵詞:

foreach (const QString &path, app.libraryPaths())
    do_something(path);
					

另請參閱 setLibraryPaths (), addLibraryPath (), removeLibraryPath (), QLibrary ,和 如何創建 Qt 插件 .

[虛擬] bool QCoreApplication:: notify ( QObject * receiver , QEvent * event )

發送 event to receiver : receiver ->event( event )。返迴來自接收者的事件處理程序返迴的值。注意:此函數被調用,對於被發送給任何綫程中的任何對象的所有事件而言。

For certain types of events (e.g. mouse and key events), the event will be propagated to the receiver's parent and so on up to the top-level object if the receiver is not interested in the event (i.e., it returns false).

有 5 種不同辦法可以處理事件。重實現此虛函數隻是其中之一。以下列齣所有 5 種途徑:

  1. Reimplementing paintEvent(), mousePressEvent() and so on. This is the commonest, easiest and least powerful way.
  2. 重實現此函數。這非常強大,提供完全控製;但每次隻可以激活一個子類。
  3. 安裝事件過濾器在 QCoreApplication::instance ()。這種事件過濾器能夠處理所有 Widget 的所有事件,因此它就像重實現 notify() 般強大;此外,擁有多個應用程序全局事件過濾器也是可能的。全局事件過濾器甚至可以看到鼠標事件對於 被禁用 Widget 。注意,僅存活在主綫程中的對象纔能調用應用程序事件過濾器。
  4. 重實現 QObject::event () (作為 QWidget 做的)。若這樣做以獲得 Tab 鍵按下,和在任何特定 Widget 事件過濾器之前看到事件。
  5. 在對象上安裝事件過濾器。這種事件過濾器獲取所有事件,包括 Tab 和 Shift+Tab 鍵按下事件,隻要它們不改變聚焦 Widget。

另請參閱 QObject::event () 和 installEventFilter ().

[static] void QCoreApplication:: postEvent ( QObject * receiver , QEvent * event )

添加事件 event ,采用對象 receiver 作為事件接收者,到事件隊列並立即返迴。

必須在堆上分配事件,因為 Post 事件隊列將擁有事件的所有權,且事件一旦已被張貼就會被刪除。它是 not safe 去訪問事件,在它被張貼之後。

當控製返迴到主事件循環時,存儲在隊列中的所有事件將被發送使用 notify () 函數。

Events are processed in the order posted. For more control over the processing order, use the postEvent() overload below, which takes a priority argument. This function posts all event with a Qt::NormalEventPriority .

注意: 此函數是 綫程安全 .

另請參閱 sendEvent (), notify (),和 sendPostedEvents ().

[static] void QCoreApplication:: postEvent ( QObject * receiver , QEvent * event , int priority )

此函數重載 postEvent ().

添加事件 event ,采用對象 receiver 作為事件接收者,到事件隊列並立即返迴。

必須在堆上分配事件,因為 Post 事件隊列將擁有事件的所有權,且事件一旦已被張貼就會被刪除。它是 not safe 去訪問事件,在它被張貼之後。

當控製返迴到主事件循環時,存儲在隊列中的所有事件將被發送使用 notify () 函數。

事件按降序排序 priority 次序,即,事件具有高 priority 隊列前於事件具有較低 priority priority 可以是任何整數值,即,在 INT_MAX 和 INT_MIN (包括在內) 之間;見 Qt::EventPriority 瞭解更多細節。事件具有相等 priority 將按張貼次序處理。

注意: 此函數是 綫程安全 .

該函數在 Qt 4.3 引入。

另請參閱 sendEvent (), notify (), sendPostedEvents (),和 Qt::EventPriority .

[static] void QCoreApplication:: processEvents ( QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents)

處理所有調用綫程待決事件,根據指定 flags 直到沒有更多要處理事件為止。

可以偶爾調用此函數當程序忙於履行長時間操作時 (如:拷貝文件)。

In event you are running a local loop which calls this function continuously, without an event loop, the DeferredDelete 事件將不被處理。這會影響 Widget 的行為,如 QToolTip 依賴 DeferredDelete 事件纔能正確運行。替代是調用 sendPostedEvents() 從本地循環內。

調用此函數僅處理調用綫程的事件。

注意: 此函數是 綫程安全 .

另請參閱 exec (), QTimer , QEventLoop::processEvents (), flush (),和 sendPostedEvents ().

[static] void QCoreApplication:: processEvents ( QEventLoop::ProcessEventsFlags flags , int maxtime )

此函數重載 processEvents ().

處理調用綫程待決事件以 maxtime 毫秒為單位或直到沒有更多要處理事件為止,以較短者為準。

You can call this function occasionally when you program is busy doing a long operation (e.g. copying a file).

調用此函數僅處理調用綫程的事件。

注意: 此函數是 綫程安全 .

另請參閱 exec (), QTimer ,和 QEventLoop::processEvents ().

[static slot] void QCoreApplication:: quit ()

告訴應用程序要退齣采用返迴代碼 0 (當成功時)。相當於調用 QCoreApplication::exit (0).

它是共有的連接 QApplication::lastWindowClosed () 信號到 quit(),且還經常連接,如 QAbstractButton::clicked () 或信號在 QAction , QMenu ,或 QMenuBar 到它。

範例:

QPushButton *quitButton = new QPushButton("Quit");
connect(quitButton, SIGNAL(clicked()), &app, SLOT(quit()));
					

另請參閱 exit (), aboutToQuit (),和 QApplication::lastWindowClosed ().

[static] void QCoreApplication:: removeLibraryPath (const QString & path )

移除 path 從庫路徑列錶。若 path 為空或不在路徑列錶中,列錶不改變。

另請參閱 addLibraryPath (), libraryPaths (),和 setLibraryPaths ().

[static] void QCoreApplication:: removePostedEvents ( QObject * receiver )

Removes all events posted using postEvent () 對於 receiver .

事件 not 被分派,取而代之,從隊列移除它們。應該從不需要調用此函數。若調用它,注意殺除事件可能導緻 receiver 破壞一個或多個不變體。

注意: 此函數是 綫程安全 .

[static] void QCoreApplication:: removePostedEvents ( QObject * receiver , int eventType )

此函數重載 removePostedEvents ().

移除所有事件為給定 eventType 的張貼是使用 postEvent () 對於 receiver .

事件 not 被分派,取而代之,從隊列移除它們。應該從不需要調用此函數。若調用它,注意殺除事件可能導緻 receiver 破壞一個或多個不變體。

receiver 為 null,事件為 eventType 被移除從所有對象。若 eventType 為 0,移除所有事件從 receiver .

注意: 此函數是 綫程安全 .

該函數在 Qt 4.3 引入。

[static] void QCoreApplication:: removeTranslator ( QTranslator * translationFile )

移除翻譯文件 translationFile 從此應用程序使用的翻譯文件列錶 (它不會從文件係統中刪除翻譯文件)。

另請參閱 installTranslator (), translate (),和 QObject::tr ().

[static] bool QCoreApplication:: sendEvent ( QObject * receiver , QEvent * event )

發送事件 event 直接到接收者 receiver ,使用 notify () 函數。返迴從事件處理程序返迴的值。

事件 not 被刪除,當事件被發送後。正常途徑是在堆棧上創建事件,例如:

QMouseEvent event(QEvent::MouseButtonPress, pos, 0, 0, 0);
QApplication::sendEvent(mainWindow, &event);
					

另請參閱 postEvent () 和 notify ().

[static] void QCoreApplication:: sendPostedEvents ( QObject * receiver , int event_type )

立即分派之前隊列的所有事件采用 QCoreApplication::postEvent () 和為對象 receiver 和事件類型 event_type .

來自窗口係統的事件 not 通過此函數分派,而是通過 processEvents ().

receiver 為 null,事件為 event_type 為所有對象發送。若 event_type 為 0,所有事件被發送為 receiver .

注意: This method must be called from the same thread as its QObject 參數, receiver .

另請參閱 flush () 和 postEvent ().

[static] void QCoreApplication:: sendPostedEvents ()

此函數重載 sendPostedEvents ().

Dispatches all posted events, i.e. empties the event queue.

[static] void QCoreApplication:: setAttribute ( Qt::ApplicationAttribute attribute , bool on = true)

設置屬性 attribute if on 為 true;否則清零屬性。

One of the attributes that can be set with this method is Qt::AA_ImmediateWidgetCreation . It tells Qt to create toplevel windows immediately. Normally, resources for widgets are allocated on demand to improve efficiency and minimize resource usage. Therefore, if it is important to minimize resource consumption, do not set this attribute.

另請參閱 testAttribute ().

EventFilter QCoreApplication:: setEventFilter ( EventFilter filter )

Replaces the event filter function for the QCoreApplication with filter and returns the pointer to the replaced event filter function. Only the current event filter function is called. If you want to use both filter functions, save the replaced EventFilter in a place where yours can call it.

The event filter function set here is called for all messages received by all threads meant for all Qt objects. It is not called for messages that are not meant for Qt objects.

The event filter function should return true if the message should be filtered, (i.e. stopped). It should return false to allow processing the message to continue.

By default, no event filter function is set (i.e., this function returns a null EventFilter the first time it is called).

注意: The filter function set here receives native messages, i.e. MSG or XEvent structs, that are going to Qt objects. It is called by QCoreApplication::filterEvent (). If the filter function returns false to indicate the message should be processed further, the native message can then be translated into a QEvent 並處理通過標準 Qt event filering, e.g. QObject::installEventFilter ().

注意: The filter function set here is different form the filter function set via QAbstractEventDispatcher::setEventFilter (), which gets all messages received by its thread, even messages meant for objects that are not handled by Qt.

另請參閱 QObject::installEventFilter () 和 QAbstractEventDispatcher::setEventFilter ().

[static] void QCoreApplication:: setLibraryPaths (const QStringList & paths )

把要搜索的目錄列錶 (當加載庫時) 設為 paths 。所有現有路徑被刪除且路徑列錶將由給定路徑組成按 paths .

In Symbian this function is only useful for setting paths for finding Qt extension plugin stubs, since the OS can only load libraries from the /sys/bin 目錄。

另請參閱 libraryPaths (), addLibraryPath (), removeLibraryPath (),和 QLibrary .

[static] bool QCoreApplication:: startingUp ()

Returns true if an application object has not been created yet; otherwise returns false.

另請參閱 closingDown ().

[static] bool QCoreApplication:: testAttribute ( Qt::ApplicationAttribute attribute )

Returns true if attribute attribute 有設置;否則返迴 false。

另請參閱 setAttribute ().

[static] QString QCoreApplication:: translate (const char * context , const char * sourceText , const char * disambiguation , 編碼 encoding , int n )

返迴翻譯文本為 sourceText ,通過查詢已安裝的翻譯文件。搜索翻譯文件是從最近安裝的文件開始到第一安裝文件。

QObject::tr () 和 QObject::trUtf8 () provide this functionality more conveniently.

context 通常是類名 (如 MyDialog) 和 sourceText 是英文文本或簡短標識文本。

disambiguation 是標識字符串,當相同 sourceText 用於同一上下文中的不同角色。默認情況下,它為 null。

QTranslator and QObject::tr () 文檔編製,瞭解有關上下文、消除歧義和注釋的更多信息。

encoding indicates the 8-bit encoding of character strings.

n 用於結閤 %n 以支持復數形式。見 QObject::tr () 瞭解細節。

若沒有翻譯文件包含要翻譯的 sourceText in context ,此函數返迴 QString 相當於 sourceText . The encoding of sourceText is specified by encoding ; it defaults to CodecForTr .

此函數不是虛擬的。可以使用替代翻譯技術通過子類化 QTranslator .

警告: 此方法纔可重入若有安裝所有翻譯器 before 調用此方法。不支持在履行翻譯時安裝 (或刪除) 翻譯器。這樣做,最可能導緻崩潰 (或其它不期望行為)。

注意: 此函數是 可重入 .

該函數在 Qt 4.5 引入。

另請參閱 QObject::tr (), installTranslator (),和 QTextCodec::codecForTr ().

[static] QString QCoreApplication:: translate (const char * context , const char * sourceText , const char * disambiguation = 0, 編碼 encoding = CodecForTr)

此函數重載 translate ().

[虛擬] bool QCoreApplication:: winEventFilter ( MSG * msg , long * result )

The message procedure calls this function for every message received. Reimplement this function if you want to process window messages msg that are not processed by Qt. If you don't want the event to be processed by Qt, then return true and set result to the value that the window procedure should return. Otherwise return false.

It is only directly addressed messages that are filtered. To handle system wide messages, such as messages from a registered hot key, you need to install an event filter on the event dispatcher, which is returned from QAbstractEventDispatcher::instance ().

相關非成員

void qAddPostRoutine ( QtCleanUpFunction ptr )

添加的全局例程將調用自 QApplication 析構函數。此函數通常用於為程序範圍功能添加清理例程。

函數的指定通過 ptr 不應接受自變量且不應返迴任何東西。例如:

static int *global_ptr = 0;
static void cleanup_ptr()
{
    delete [] global_ptr;
    global_ptr = 0;
}
void init_ptr()
{
    global_ptr = new int[100];      // allocate data
    qAddPostRoutine(cleanup_ptr);   // delete later
}
					

注意,對於應用程序 (或模塊) 範圍的清理,qAddPostRoutine() 常常不閤適。例如,若程序被拆分成動態加載的模塊,則相關模塊可能早被卸載先於 QApplication destructor is called.

對於模塊和庫,使用引用計數的初始化管理器或 Qt 的父級/子級刪除機製可能更好。這裏是使用父級/子級機製在正確時間調用清理函數的私有類範例:

class MyPrivateInitStuff : public QObject
{
public:
    static MyPrivateInitStuff *initStuff(QObject *parent)
    {
        if (!p)
            p = new MyPrivateInitStuff(parent);
        return p;
    }
    ~MyPrivateInitStuff()
    {
        // cleanup goes here
    }
private:
    MyPrivateInitStuff(QObject *parent)
        : QObject(parent)
    {
        // initialization goes here
    }
    MyPrivateInitStuff *p;
};
					

通過選擇正確父級對象,這經常可以在正確時刻清理模塊數據。

宏文檔編製

Q_DECLARE_TR_FUNCTIONS ( context )

Q_DECLARE_TR_FUNCTIONS() 宏聲明並實現瞭 2 個翻譯函數 tr() and trUtf8() ,采用這些簽名:

static inline QString tr(const char *sourceText,
                         const char *comment = 0);
static inline QString trUtf8(const char *sourceText,
                             const char *comment = 0);
					

此宏很有用,若想要使用 QObject::tr () 或 QObject::trUtf8 () 當類未繼承自 QObject .

Q_DECLARE_TR_FUNCTIONS() 必須齣現在類定義最頂部 (先於第 1 個 public: or protected: )。例如:

class MyMfcView : public CView
{
    Q_DECLARE_TR_FUNCTIONS(MyMfcView)
public:
    MyMfcView();
    ...
};
					

The context parameter is normally the class name, but it can be any string.

另請參閱 Q_OBJECT , QObject::tr (),和 QObject::trUtf8 ().