Qt:QCoreApplication
阅读原文时间:2023年07月11日阅读:1

QCoreApplication提供了有关当前运行程序的相关信息,当前程序应当是非GUI程序。对于GUI程序,应该用QGuiApplication,而对于采用了Qt Widget模块的程序,应该使用QApplication。不过这三者的继承关系是,QCoreApplication → QGuiApplicatioin → QApplication,所以后两种类也可以使用QCoreApplication的相关方法,如applicationDirPath()——可执行应用文件所在目录;applicationFilePath()——可执行应用文件路径。

QCoreApplication负责管理main事件循环,其中包含了来自OS的所有事件。此外,它也负责管理程序的初始化和收尾工作,包括系统和应用两方面的设置。

1)事件循环与事件处理

事件循环通过exec()启动。长期运行的程序可以调用processEvents()来保证程序的响应性。

通常情况下,推荐在main()方法中尽可能早地构造QCoreApplication、QGuiApplication、QApplication对象。exec()方法直到事件循环通过quit()退出时才会返回。

类中有许多很方便的静态方法,例如,通过instance()来获取QCoreApplication对象。事件通过sendEvent()发送,也可以通过postEvent()来发送一个事件队列。正在等待的事件可以通过removePostedEvents()移除,或者通过sendPostedEvents()进行部署。

2)程序与库的路径

程序路径通过applicationDirPath() 和 applicationFilePath()来确定,分别是程序目录和程序文件的路径。

库路径通过libraryPaths()来确定,通过setLibraryPaths()、addLibraryPath() 和 removeLibraryPath()操作库路径。

3)国际化与翻译

翻译文件通过 installTranslator() 和 removeTranslator()进行添加和移除。

程序字符串通过 translate() 进行翻译。

QObject::tr()和QObject::trUtf8()函数是通过translate()实现的。

4)命令行参数

传递到QCoreApplication构造函数中的命令行参数通过arguments()函数进行访问。

5)本地设置

在Unix/Linux系统下,Qt采用默认采用系统本地配置,这在使用POSIX系统的函数中可能会引发混乱。为了解决该问题,可以调用POSIX方法 setlocale( LC_NUMERIC , "C" ),调用时机是在初始化  QApplication 、QGuiApplication 、 QCoreApplication之后。

Header:

#include

qmake:

QT += core

Inherits:

QObject

Inherited By:

QGuiApplication

QCoreApplication(int &argc, char **_argv_)

构造一个Qt Core程序。Core程序没有用户界面,常见的有控制台程序、服务器程序。

argc与argv都是传入的参数,即命令行参数,通过arguments()可以用更方便的形式处理。

类型

字段

说明

QString

applicationName

程序名

QString

applicationVersion

程序版本

QString

organizationDomain

组织域

QString

organizationName

组织名

bool

quitLockEnabled

是否允许QEventLoopLocker

返回值类型

方法

说明

void

addLibraryPath(QString path)

添加库路径到Library path list开头,以确保该路径在搜索库时第一个被搜索到。

QString

applicationDirPath()

应用可执行文件所在的目录的路径。

QString

applicationFilePath()

应用可执行文件的路径

qint64

applicationPid()

应用所属的当前进程ID

QString

applicationVersion()

应用的版本

QStringList

arguments()

命令行参数的List

bool

closingDown()

当应用要被结束时,返回true

QAbstractEventDispatcher *

eventDispatcher()

 

int

exec()

进入主程序,启动主循环,等待调用exit()。

返回值是exit()传来的值。

void

exit(int returnCode = 0)

以指定returnCode退出应用,0表示正常退出。

bool

installTranslator(QTranslator *_translationFile_)

添加翻译器

QCoreApplication *

instance()

应用对象本身

bool

isQuitLockEnabled()

 

isSetuidAllowed()

 

QStringList

libraryPaths()

库路径List

QString

organizationDomain()

 

QString

organizationName()

 

void

postEvent(QObject *_receiver_, QEvent *_event_, int priority = Qt::NormalEventPriority)

添加事件

void

processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents)

processEvents(QEventLoop::ProcessEventsFlags flags, int ms)

处理默写待运行事件

void

removeLibraryPath(const QString &path)

移除库路径

void

removePostedEvents(QObject *_receiver_, int eventType = 0)

移除所有用postEvent()发送给receiver的事件

bool

removeTranslator(QTranslator *_translationFile_)

移除翻译器

bool

sendEvent(QObject *_receiver_, QEvent *_event_)

直接发送事件给receiver

void

sendPostedEvents(QObject *_receiver_ = nullptr, int event_type = 0)

立即处理之前通过postEvent()加入队列中的事件

void

setApplicationName(const QString &application)

设置一些信息

setApplicationVersion(const QString &version)

setAttribute(Qt::ApplicationAttribute attribute, bool on = true)

setEventDispatcher(QAbstractEventDispatcher *_eventDispatcher_)

setLibraryPaths(QStringList paths)

setOrganizationDomain(QString orgDomain)

setOrganizationName(QString orgName)

setQuitLockEnabled(bool enabled)

setSetuidAllowed(bool allow)

bool

startingUp()

如果应用对象还未创建,返回true

bool

testAttribute(Qt::ApplicationAttribute attribute)

如果attribute设置了,返回true

QString

translate(const char *_context_, const char *_sourceText_, const char *_disambiguation_ = nullptr, int n = -1)

返回翻译文本

返回值类型

方法

说明

void

installNativeEventFilter(QAbstractNativeEventFilter *_filterObj_)

事件筛选

virtual bool

notify(QObject *_receiver_, QEvent *_event_)

发送事件给receiver

void

removeNativeEventFilter(QAbstractNativeEventFilter *_filterObject_)

移除事件筛选

void

aboutToQuit()

当程序将要退出 主循环 时发送该信号。

void

applicationNameChanged()

当一些信息改变时发送信号

void

applicationVersionChanged()

void

organizationDomainChanged()

void

organizationNameChanged()

void

quit()

退出,并返回0(表示成功)。等同于调用QCoreApplication::exit(0)