Qt:QCoreApplication

0、说明

QCoreApplication提供了有关当前运行程序的相关信息,当前程序应当是非GUI程序。对于GUI程序,应该用QGuiApplication,而对于采用了Qt Widget模块的程序,应该使用QApplication。不过这三者的继承关系是,QCoreApplicationQGuiApplicatioinQApplication,所以后两种类也可以使用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之后。

1、模块和加载项

Header: #include <QCoreApplication>
qmake: QT += core
Inherits: QObject
Inherited By:

QGuiApplication

2、构造

QCoreApplication(int &argc, char **argv)

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

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

 

4、实例字段

类型

字段

说明

QString applicationName 程序名
QString applicationVersion 程序版本
QString organizationDomain 组织域
QString organizationName 组织名
bool quitLockEnabled 是否允许QEventLoopLocker

 

5、静态方法

返回值类型

方法

说明

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) 返回翻译文本

 

6、实例方法

返回值类型

方法

说明

void installNativeEventFilter(QAbstractNativeEventFilter *filterObj) 事件筛选
virtual bool notify(QObject *receiver, QEvent *event) 发送事件给receiver
void removeNativeEventFilter(QAbstractNativeEventFilter *filterObject) 移除事件筛选

 

7、信号

void aboutToQuit() 当程序将要退出 主循环 时发送该信号。
void applicationNameChanged() 当一些信息改变时发送信号
void applicationVersionChanged()
void organizationDomainChanged()
void organizationNameChanged()

8、槽

void quit() 退出,并返回0(表示成功)。等同于调用QCoreApplication::exit(0)
posted @   ShineLe  阅读(2600)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
点击右上角即可分享
微信分享提示