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 属性 , bool on = true) |
| void | setLibraryPaths (const QStringList & paths ) |
| void | setOrganizationDomain (const QString & orgDomain ) |
| void | setOrganizationName (const QString & orgName ) |
| bool | startingUp () |
| bool | testAttribute (Qt::ApplicationAttribute 属性 ) |
| 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 , 信号量范例 ,和 等待条件范例 .
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 ().
A function with the following signature that can be used as an event filter:
bool myEventFilter(void *message, long *result);
另请参阅 setEventFilter ().
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 .
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 .
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 .
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 .
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 对象。
[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 返回给调用者 -- 它停止事件处理。
发送 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 种途径:
另请参阅 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 .
注意: 此函数是 thread-safe .
另请参阅 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 将按张贴次序处理。
注意: 此函数是 thread-safe .
该函数在 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() 从本地循环内。
调用此函数仅处理调用线程的事件。
注意: 此函数是 thread-safe .
另请参阅 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).
调用此函数仅处理调用线程的事件。
注意: 此函数是 thread-safe .
另请参阅 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 破坏一个或多个不变体。
注意: 此函数是 thread-safe .
[static]
void
QCoreApplication::
removePostedEvents
(
QObject
*
receiver
,
int
eventType
)
此函数重载 removePostedEvents ().
移除所有事件为给定 eventType 的张贴是使用 postEvent () 对于 receiver .
事件 not 被分派,取而代之,从队列移除它们。应该从不需要调用此函数。若调用它,注意杀除事件可能导致 receiver 破坏一个或多个不变体。
若 receiver 为 null,事件为 eventType 被移除从所有对象。若 eventType 为 0,移除所有事件从 receiver .
注意: 此函数是 thread-safe .
该函数在 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 .
[static]
void
QCoreApplication::
sendPostedEvents
()
此函数重载 sendPostedEvents ().
Dispatches all posted events, i.e. empties the event queue.
[static]
void
QCoreApplication::
setAttribute
(
Qt::ApplicationAttribute
属性
,
bool
on
= true)
设置属性 属性 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 ().
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
属性
)
Returns true if 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 ().
添加的全局例程将调用自 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() 宏声明并实现了 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() 必须出现在类定义最顶部 (先于第一个
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 ().