QCoreApplication 类

QCoreApplication 类为没有 UI 的 Qt 应用程序提供事件循环。 更多...

头: #include <QCoreApplication>
qmake: QT += core
继承: QObject
继承者:

QGuiApplication

特性

公共函数

QCoreApplication (int & argc , char ** argv )
~QCoreApplication ()
void installNativeEventFilter (QAbstractNativeEventFilter * filterObj )
virtual bool notify (QObject * receiver , QEvent * event )
void removeNativeEventFilter (QAbstractNativeEventFilter * filterObject )

公共槽

void quit ()

信号

void aboutToQuit ()

静态公共成员

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

重实现保护函数

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

Q_COREAPP_STARTUP_FUNCTION (QtStartUpFunction ptr )
Q_DECLARE_TR_FUNCTIONS ( context )

详细描述

QCoreApplication 类为没有 UI 的 Qt 应用程序提供事件循环。

This class is used by non-GUI applications to provide their event loop. For non-GUI application that uses Qt, there should be exactly one QCoreApplication object. For GUI applications, see QGuiApplication 。对于使用 Qt Widgets 模块的应用程序,见 QApplication .

QCoreApplication contains the main event loop, where all events from the operating system (e.g., timer and network events) and other sources are processed and dispatched. It also handles the application's initialization and finalization, as well as system-wide and application-wide settings.

事件循环和事件处理

开始事件循环通过调用 exec ()。长时间运行的操作可以调用 processEvents () 以保持应用程序的响应速度。

In general, we recommend that you create a QCoreApplication , QGuiApplication QApplication 对象在您的 main() 函数要尽可能早。 exec () 不会返回直到事件循环退出;如:当 quit () 被调用。

Several static convenience functions are also provided. The QCoreApplication object is available from instance ()。可以发送事件采用 sendEvent () 或张贴到事件队列采用 postEvent ()。待决事件可以移除采用 removePostedEvents () 或分派采用 sendPostedEvents ().

类提供 quit () 槽和 aboutToQuit () 信号。

应用程序和库路径

应用程序有 applicationDirPath () 和 applicationFilePath ()。库路径 (见 QLibrary ) 可以被检索采用 libraryPaths () 和操纵通过 setLibraryPaths (), addLibraryPath (),和 removeLibraryPath ().

国际化和翻译

可以添加或移除翻译文件使用 installTranslator () 和 removeTranslator ()。可以翻译应用程序字符串使用 translate ()。 QObject::tr () 和 QObject::trUtf8() 函数的实现根据 translate ().

访问命令行自变量

The command line arguments which are passed to QCoreApplication 's constructor should be accessed using the arguments () 函数。

注意: QCoreApplication removes option -qmljsdebugger="..." 。它剖析自变量为 qmljsdebugger ,然后删除此选项及其自变量。

对于更高级命令行选项处理,创建 QCommandLineParser .

区域设置

在 Unix/Linux,Qt 默认配置为使用系统区域设置。这会导致冲突当使用 POSIX 函数时,例如,当在数据类型 (譬如:浮点数和字符串) 之间转换时,由于表示法可能因区域设置不同而异。要解决此问题,调用 POSIX 函数 setlocale(LC_NUMERIC,"C") 恰好先于初始化 QApplication , QGuiApplication or QCoreApplication to reset the locale that is used for number formatting to "C"-locale.

另请参阅 QGuiApplication , QAbstractEventDispatcher , QEventLoop , 信号量范例 ,和 等待条件范例 .

特性文档编制

applicationName : QString

此特性保持该应用程序的名称

值用于 QSettings 类当使用空构造函数构造它时。这节省必须重复此信息每次 QSettings 对象被创建。

若未设置,应用程序名称默认为可执行文件名称 (从 5.0 起)。

访问函数:

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

另请参阅 organizationName , organizationDomain , applicationVersion ,和 applicationFilePath ().

applicationVersion : QString

此特性保持该应用程序的版本

若未设置,应用程序版本默认为由主应用程序可执行文件或包确定的特定平台值 (从 Qt 5.9 起):

平台
Windows (经典桌面) VERSIONINFO 资源的 PRODUCTVERSION 参数
通用 Windows 平台 应用程序包清单的版本属性
macOS, iOS, tvOS, watchOS 信息特性列表的 CFBundleVersion 属性
Android AndroidManifest.xml 清单元素的 android:versionName 特性

在其它平台,默认为空字符串。

该特性在 Qt 4.4 引入。

访问函数:

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

另请参阅 applicationName , organizationName ,和 organizationDomain .

organizationDomain : QString

此特性保持编写此应用程序的 Internet 组织域

值用于 QSettings 类当使用空构造函数构造它时。这节省必须重复此信息每次 QSettings 对象被创建。

在 Mac, QSettings 使用 organizationDomain() 作为组织若它不是空字符串;否则它使用 organizationName ()。在所有其它平台, QSettings 使用 organizationName () 作为组织。

访问函数:

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

另请参阅 organizationName , applicationName ,和 applicationVersion .

organizationName : QString

此特性保持编写此应用程序的组织名称

值用于 QSettings 类当使用空构造函数构造它时。这节省必须重复此信息每次 QSettings 对象被创建。

在 Mac, QSettings 使用 organizationDomain() 作为组织若它不是空字符串;否则它使用 organizationName()。在所有其它平台, QSettings 使用 organizationName() 作为组织。

访问函数:

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

另请参阅 organizationDomain and applicationName .

quitLockEnabled : bool

此特性保持是否使用 QEventLoopLocker 特征,可能导致应用程序离开。

默认为 true .

访问函数:

bool isQuitLockEnabled ()
void setQuitLockEnabled (bool enabled )

另请参阅 QEventLoopLocker .

成员函数文档编制

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

构造 Qt 核心应用程序。核心应用程序是没有 GUI (图形用户界面) 的应用程序。这种应用程序用于控制台或用作服务器进程。

argc and argv 自变量由应用程序处理,并以更方便形式使之可用通过 arguments () 函数。

警告: 数据的引用通过 argc and argv must stay valid for the entire lifetime of the QCoreApplication object. In addition, argc 必须大于 0 且 argv 必须包含至少一有效字符串。

QCoreApplication:: ~QCoreApplication ()

销毁 QCoreApplication 对象。

[signal] void QCoreApplication:: aboutToQuit ()

此信号被发射当应用程序即将离开主事件循环 (如:当事件循环级别降至 0 时)。这可能发生之前有调用 quit () 从应用程序内或当用户关闭整个桌面会话时。

信号特别有用,若应用程序必须在最后一秒做一些清理。注意,在此状态下不可能进行用户交互。

注意: 这是私有信号。它可以用于信号连接,但不能由用户发射。

另请参阅 quit ().

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

前置 path 到库路径列表的开头,确保库被首先搜索。若 path 为空或已在路径列表中,路径列表不改变。

The default path list consists of a single entry, the installation directory for plugins. The default installation directory for plugins is INSTALL/plugins ,其中 INSTALL is the directory where Qt was installed.

库路径被重置成默认当实例化的 QCoreApplication 被销毁。

另请参阅 removeLibraryPath (), libraryPaths (),和 setLibraryPaths ().

[static] QString QCoreApplication:: applicationDirPath ()

返回包含应用程序可执行文件的目录。

例如,若有安装 Qt 在 C:\Qt 目录,和运行 regexp 范例,此函数将返回 C:/Qt/examples/tools/regexp。

在 macOS 和 iOS,这指向实际包含可执行文件的目录,可能位于应用程序捆绑中 (若应用程序有捆绑)。

警告: 在 Linux,此函数将试着获取路径从 /proc 文件系统。若失败,则假定 argv[0] 包含可执行文件的绝对文件名。函数还假定当前目录未被应用程序所改变。

另请参阅 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 ()

返回命令行自变量列表。

Usually arguments().at(0) is the program name, arguments().at(1) is the first argument, and arguments().last() is the last argument. See the note below about Windows.

调用此函数很慢 - 应将结果存储在变量中,当剖析命令行时。

警告: On Unix, this list is built from the argc and argv parameters passed to the constructor in the main() function. The string-data in argv is interpreted using QString::fromLocal8Bit (); hence it is not possible to pass, for example, Japanese command line arguments on a system that runs in a Latin1 locale. Most modern Unix systems do not have this limitation, as they are Unicode-based.

On Windows, the list is built from the argc and argv parameters only if modified argv/argc parameters are passed to the constructor. In that case, encoding problems might occur.

Otherwise, the arguments() are constructed from the return value of GetCommandLine() . As a result of this, the string given by arguments().at(0) might not be the program name on Windows, depending on how the application was started.

该函数在 Qt 4.1 引入。

另请参阅 applicationFilePath () 和 QCommandLineParser .

[static] bool QCoreApplication:: closingDown ()

返回 true 若应用程序对象正在被销毁;否则返回 false .

另请参阅 startingUp ().

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

重实现自 QObject::event ().

[static] QAbstractEventDispatcher *QCoreApplication:: eventDispatcher ()

Returns a pointer to the event dispatcher object for the main thread. If no event dispatcher exists for the thread, this function returns 0.

另请参阅 setEventDispatcher ().

[static] int QCoreApplication:: exec ()

进入主事件循环并等待,直到 exit () 被调用。返回值被传递给 exit () (其为 0 若 exit () 被调用凭借 quit ()).

有必要调用此函数以启动事件处理。主事件循环从窗口系统接收事件,并将其分派给应用程序 Widget。

要使应用程序履行空闲处理 (通过执行特殊函数,当没有待决事件时),使用 QTimer 采用 0 超时。更高级空闲处理方案可以达成使用 processEvents ().

推荐把清理代码连接到 aboutToQuit() 信号,而不是将它放入应用程序的 main() 函数,因为在某些平台,exec() 调用可能不返回。例如在 Windows,当用户注销时,系统在 Qt 关闭所有顶层窗口后终止过程。因此,不保证应用程序会有时间退出其事件循环并执行代码结束 main() 函数在 exec() 调用之后。

另请参阅 quit (), exit (), processEvents (),和 QApplication::exec ().

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

告诉应用程序采用返回代码去退出。

调用此函数之后,应用程序离开主事件循环并返回从调用 exec ()。 exec () 函数返回 returnCode 。若事件循环未运行,此函数什么都不做。

按约定, returnCode 0 意味着成功,而任何非零值指示出错。

注意:不像同名 C 库函数,此函数 does 返回给调用者 -- 它停止事件处理。

另请参阅 quit () 和 exec ().

void QCoreApplication:: installNativeEventFilter ( QAbstractNativeEventFilter * filterObj )

安装事件过滤器 filterObj 为所有本机接收事件,通过主线程中的应用程序。

事件过滤器 filterObj 接收事件凭借其 nativeEventFilter() 函数,在主线程中收到的所有本机事件都会调用它。

QAbstractNativeEventFilter::nativeEventFilter () function should return true if the event should be filtered, i.e. stopped. It should return false to allow normal Qt processing to continue: the native event can then be translated into a QEvent and handled by the standard Qt event 过滤,如 QObject::installEventFilter ().

若安装了多个事件过滤器,则首先激活最后安装的过滤器。

注意: 这里设置的过滤器函数接收本机消息,即:MSG 或 XCB 事件结构。

注意: 本机事件过滤器将被应用程序所禁用当 Qt::AA_PluginApplication 属性有设置。

为获得最大可移植性,应始终试着使用 QEvent and QObject::installEventFilter () 每当可能时。

该函数在 Qt 5.0 引入。

另请参阅 QObject::installEventFilter ().

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

添加翻译文件 translationFile 到用于翻译的翻译文件列表。

可以安装多个翻译文件。按照安装它们时的反向次序搜索翻译,因此,首先搜索最近安装的翻译文件,最后搜索首先安装的翻译文件。一旦发现包含匹配字符串的翻译,就停止搜索。

安装或移除 QTranslator ,或改变安装的 QTranslator 生成 LanguageChange 事件为 QCoreApplication 实例。 QApplication 实例将事件传播给所有顶层 Widget,重实现 changeEvent 可以重新翻译用户界面通过传递用户可见字符串凭借 tr () 函数到各自的特性 Setter。由 Qt Designer 生成的用户界面类提供 retranslateUi() 函数可以被调用。

函数返回 true 当成功时和 false 当失败时。

另请参阅 removeTranslator (), translate (), QTranslator::load (),和 动态翻译 .

[static] QCoreApplication *QCoreApplication:: instance ()

返回指针指向应用程序的 QCoreApplication (或 QGuiApplication / QApplication ) 实例。

若没有分配实例, null 被返回。

[static] bool QCoreApplication:: isSetuidAllowed ()

返回 true 若允许应用程序在 UNIX 平台运行 setuid。

该函数在 Qt 5.3 引入。

另请参阅 QCoreApplication::setSetuidAllowed ().

[static] QStringList QCoreApplication:: libraryPaths ()

返回将搜索的应用程序路径列表当动态加载库时。

此函数的返回值可能改变当 QCoreApplication 被创建。不推荐调用它先于创建 QCoreApplication 。应用程序可执行文件目录 ( not 工作目录) 是列表的一部分若已知。为使它知道 QCoreApplication 已构造,所以它将使用 argv[0] 来查找它。

Qt 提供默认库路径,但也可以设置它们使用 qt.conf 文件。在此文件中指定的路径将覆盖默认值。注意,若 qt.conf 文件在应用程序可执行文件目录下,可能找不到它直到 QCoreApplication 被创建。若当调用该函数时找不到它,将使用默认库路径。

列表将包括插件安装目录,若存在 (插件的默认安装目录为 INSTALL/plugins ,其中 INSTALL 是 Qt 安装目录)。冒号分隔的条目对于 QT_PLUGIN_PATH 环境变量总是被添加。插件安装目录 (且其存在) 可能改变,当应用程序可执行文件目录变为已知时。

若想要遍历列表,可以使用 foreach 伪关键词:

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

另请参阅 setLibraryPaths (), addLibraryPath (), removeLibraryPath (), QLibrary ,和 如何创建 Qt 插件 .

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

发送 event to receiver : receiver ->event( event )。返回来自接收者的事件处理程序返回的值。注意:此函数被调用,对于被发送给任何线程中的任何对象的所有事件而言。

对于某些类型的事件 (如鼠标事件和按键事件),事件会被传播给接收者的父级,依此类推直至顶层对象,若接收者对事件不感兴趣 (即:它返回 false ).

有 5 种不同办法可以处理事件。重实现此虚函数只是其中之一。以下列出所有 5 种途径:

  1. 重实现 paintEvent() , mousePressEvent() 等。这是最常见、最简单且最不强大的方式。
  2. 重实现此函数。这非常强大,提供完全控制;但每次只可以激活一个子类。
  3. 安装事件过滤器在 QCoreApplication::instance ()。这种事件过滤器能够处理所有 Widget 的所有事件,因此它就像重实现 notify() 般强大;此外,拥有多个应用程序全局事件过滤器也是可能的。全局事件过滤器甚至可以看到鼠标事件对于 被禁用 Widget 。注意,仅存活在主线程中的对象才能调用应用程序事件过滤器。
  4. 重实现 QObject::event () (作为 QWidget 做的)。若这样做以获得 Tab 键按下,和在任何特定 Widget 事件过滤器之前看到事件。
  5. 在对象上安装事件过滤器。这种事件过滤器获取所有事件,包括 Tab 和 Shift+Tab 键按下事件,只要它们不改变聚焦 Widget。

未来方向: 在 Qt 6 主线程外的对象,不会调用此函数。同时,需要该功能的应用程序应为其事件审查需求寻找其它解决方案。改变可能延伸到主线程,导致此函数被弃用。

警告: 若覆盖此函数,必须确保处理事件的所有线程停止这样做,在应用程序对象开始销毁之前。这包括可能正被使用由其它库启动的线程,但不适用于 Qt 自己的线程。

另请参阅 QObject::event () 和 installNativeEventFilter ().

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

添加事件 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 直到没有更多要处理事件为止。

可以偶尔调用此函数当程序忙于履行长时间操作时 (如:拷贝文件)。

在正在运行连续调用此函数的本地循环事件中,若没有事件循环, DeferredDelete 事件不会被处理。这会影响 Widget 的行为,如 QToolTip 依赖 DeferredDelete 事件才能正确运行。替代是调用 sendPostedEvents() 从本地循环中。

调用此函数仅处理调用线程的事件。

注意: 此函数是 thread-safe .

另请参阅 exec (), QTimer , QEventLoop::processEvents (), flush (),和 sendPostedEvents ().

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

此函数重载 processEvents ().

处理调用线程待决事件以 maxtime 毫秒为单位或直到没有更多要处理事件为止,以较短者为准。

可以偶尔调用此函数当程序忙于做长时间操作时 (如:拷贝文件)。

调用此函数仅处理调用线程的事件。

注意: 此函数是 thread-safe .

另请参阅 exec (), QTimer ,和 QEventLoop::processEvents ().

[static slot] void QCoreApplication:: quit ()

告诉应用程序要退出采用返回代码 0 (当成功时)。相当于调用 QCoreApplication::exit (0).

它是共有的连接 QGuiApplication::lastWindowClosed () 信号到 quit(),且还经常连接,如 QAbstractButton::clicked () 或信号在 QAction , QMenu ,或 QMenuBar 到它。

范例:

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

另请参阅 exit (), aboutToQuit (),和 QGuiApplication::lastWindowClosed ().

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

移除 path 从库路径列表。若 path 为空或不在路径列表中,列表无变化。

库路径被重置成默认当实例化的 QCoreApplication 被销毁。

另请参阅 addLibraryPath (), libraryPaths (),和 setLibraryPaths ().

void QCoreApplication:: removeNativeEventFilter ( QAbstractNativeEventFilter * filterObject )

移除事件 filterObject 从此对象。忽略请求,若尚未安装这种事件过滤器。

将自动移除此对象的所有事件过滤器,当此对象被销毁时。

它始终安全地移除事件过滤器,即使在事件过滤器激活期间 (即:从 nativeEventFilter() 函数)。

该函数在 Qt 5.0 引入。

另请参阅 installNativeEventFilter ().

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

移除所有事件为给定 eventType 被张贴使用 postEvent () 对于 receiver .

事件 not 被分派,取而代之,从队列移除它们。应该从不需要调用此函数。若调用它,注意杀除事件可能导致 receiver 破坏一个或多个不变体。

receiver 为 null,事件对于 eventType 被移除为所有对象。若 eventType 为 0,移除所有事件为 receiver 。 从不应调用此函数采用 eventType of 0. If you do call it in this way, be aware that killing events may cause receiver 破坏一个或多个不变体。

注意: 此函数是 thread-safe .

该函数在 Qt 4.3 引入。

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

移除翻译文件 translationFile 从此应用程序使用的翻译文件列表 (它不会从文件系统中删除翻译文件)。

函数返回 true 当成功时和 false 当失败时。

另请参阅 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 = Q_NULLPTR, int event_type = 0)

立即分派之前队列的所有事件采用 QCoreApplication::postEvent () 和为对象 receiver 和事件类型 event_type .

来自窗口系统的事件 not 通过此函数分派,而是通过 processEvents ().

receiver 为 null,事件对于 event_type 为所有对象发送。若 event_type 为 0,所有事件被发送为 receiver .

注意: 此方法必须被调用从线程以其 QObject 参数, receiver ,存活。

另请参阅 flush () 和 postEvent ().

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

设置属性 attribute if on 为 true;否则清零属性。

另请参阅 testAttribute ().

[static] void QCoreApplication:: setEventDispatcher ( QAbstractEventDispatcher * eventDispatcher )

将主线程事件分派程序设为 eventDispatcher 。这才有可能,只要尚未安装事件分派程序。也就是,先于 QCoreApplication 被实例化。此方法拥有对象的所有权。

另请参阅 eventDispatcher ().

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

把要搜索的目录列表 (当加载库时) 设为 paths 。所有现有路径被删除且路径列表将由给定路径组成按 paths .

库路径被重置成默认当实例化的 QCoreApplication 被销毁。

另请参阅 libraryPaths (), addLibraryPath (), removeLibraryPath (),和 QLibrary .

[static] void QCoreApplication:: setSetuidAllowed ( bool allow )

允许应用程序在 UNIX 平台运行 setuid 若 allow 为 true。

allow is false (the default) and Qt detects the application is running with an effective user id different than the real user id, the application will be aborted when a QCoreApplication instance is created.

Qt is not an appropriate solution for setuid programs due to its large attack surface. However some applications may be required to run in this manner for historical reasons. This flag will prevent Qt from aborting the application when this is detected, and must be set before a QCoreApplication instance is created.

注意: 强烈推荐不启用此选项,由于它引入安全风险。

该函数在 Qt 5.3 引入。

另请参阅 isSetuidAllowed ().

[static] bool QCoreApplication:: startingUp ()

返回 true 若应用程序对象尚未被创建;否则返回 false .

另请参阅 closingDown ().

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

返回 true 若属性 attribute 已设置;否则返回 false .

另请参阅 setAttribute ().

[static] QString QCoreApplication:: translate (const char * context , const char * sourceText , const char * disambiguation = Q_NULLPTR, int n = -1)

返回翻译文本为 sourceText ,通过查询已安装的翻译文件。搜索翻译文件是从最近安装的文件开始到第一安装文件。

QObject::tr () 更方便提供此功能。

context 通常是类名 (如 MyDialog) 和 sourceText 是英文文本或简短标识文本。

disambiguation is an identifying string, for when the same sourceText is used in different roles within the same context. By default, it is null.

QTranslator and QObject::tr () documentation for more information about contexts, disambiguations and comments.

n 用于结合 %n 以支持复数形式。见 QObject::tr () 了解细节。

If none of the translation files contain a translation for sourceText in context ,此函数返回 QString 相当于 sourceText .

此函数不是虚拟的。可以使用替代翻译技术通过子类化 QTranslator .

警告: This method is reentrant only if all translators are installed before calling this method. Installing or removing translators while performing translations is not supported. Doing so will most likely result in crashes or other undesirable behavior.

注意: 此函数是 可重入 .

另请参阅 QObject::tr () 和 installTranslator ().

相关非成员

void qAddPostRoutine ( QtCleanUpFunction ptr )

添加的全局例程将调用自 QCoreApplication 析构函数。此函数通常用于为程序范围功能添加清理例程。

清理例程按添加它们的反向次序被调用。

函数的指定通过 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
}
					

Note that for an application- or module-wide cleanup, qaddPostRoutine() is often not suitable. For example, if the program is split into dynamically loaded modules, the relevant module may be unloaded long before the QCoreApplication destructor is called. In such cases, if using qaddPostRoutine() is still desirable, qRemovePostRoutine () can be used to prevent a routine from being called by the QCoreApplication destructor. For example, if that routine was called before the module was unloaded.

For modules and libraries, using a reference-counted initialization manager or Qt's parent-child deletion mechanism may be better. Here is an example of a private class that uses the parent-child mechanism to call a cleanup function at the right time:

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

By selecting the right parent object, this can often be made to clean up the module's data at the right moment.

另请参阅 qRemovePostRoutine ().

void qRemovePostRoutine ( QtCleanUpFunction ptr )

移除的清理例程指定通过 ptr from the list of routines called by the QCoreApplication destructor. The routine must have been previously added to the list by a call to qAddPostRoutine (), otherwise this function has no effect.

该函数在 Qt 5.3 引入。

另请参阅 qAddPostRoutine ().

宏文档编制

Q_COREAPP_STARTUP_FUNCTION ( QtStartUpFunction ptr )

添加的全局函数调用自 QCoreApplication constructor. This macro is normally used to initialize libraries for program-wide functionality, without requiring the application to call into the library for initialization.

函数的指定通过 ptr 不应接受自变量且不应返回任何东西。例如:

// Called once QCoreApplication exists
static void preRoutineMyDebugTool()
{
    MyDebugTool* tool = new MyDebugTool(QCoreApplication::instance());
    QCoreApplication::instance()->installEventFilter(tool);
}
Q_COREAPP_STARTUP_FUNCTION(preRoutineMyDebugTool)
					

Note that the startup function will run at the end of the QCoreApplication constructor, before any GUI initialization. If GUI code is required in the function, use a timer (or a queued invocation) to perform the initialization later on, from the event loop.

QCoreApplication 被删除且另一 QCoreApplication 被创建,启动函数将被再次援引。

注意: This macro is not suitable for use in library code that is then statically linked into an application since the function may not be called at all due to being eliminated by the linker.

注意: 此函数是 可重入 .

该函数在 Qt 5.1 引入。

Q_DECLARE_TR_FUNCTIONS ( context )

The Q_DECLARE_TR_FUNCTIONS() macro declares and implements two translation functions, tr() and trUtf8() , with these signatures:

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();
    ...
};
					

context 参数通常是类名,但它可以是任何文本。

另请参阅 Q_OBJECT , QObject::tr (),和 QObject::trUtf8 ().