0、说明

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之后。

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)

最新文章

  1. iOS 浅谈:深.浅拷贝与copy.strong
  2. vmware下的linux的host only上网配置
  3. 自定义Dialog以及Dialog返回值到Activity
  4. FreeBSD基金会添加新成员,梁莉成为第一位来自微软和中国的基金会董事
  5. Qt之设置QWidget背景色
  6. Spring MVC中使用Mongodb总结
  7. C++ 採集音频流(PCM裸流)实现录音功能
  8. 我的学习笔记_Windows_HOOK计划 2009-12-03 11:19
  9. xdebug.var_display_max_data
  10. HashMap,HashTable,ConcurrentHashMap异同比较
  11. Eclipse安装git插件以及关联导入GitHub项目
  12. Codeforces 1097E. Egor and an RPG game 构造
  13. find查找文件的时间问题
  14. 【Scrapy】关于使用Scrapy框架爬虫遇到的问题1
  15. 使用vue-cli3新建一个项目,并写好基本配置
  16. ELM320 OBD(PWM) to RS232 Interpreter
  17. object与byte[]的相互转换、文件与byte数组相互转换
  18. ManagementClass(&quot;Win32_Share&quot;)之共享目录
  19. python 变量,输入,输出
  20. 信号、系统与滤波器设计(matlab)

热门文章

  1. 前端基础之SCC
  2. Linux 集群 和免秘钥登录的方法。
  3. arduino物联网学习
  4. CSP2019 Day2T2 划分
  5. JS 解构赋值
  6. 抓包神器 tcpdump 使用介绍 (转)
  7. 【转载】Locust实现集合点
  8. Java微信公众号服务器配置-验证Token
  9. Mysql批量删除和修改某个前缀的表
  10. 《PHP程序员面试笔试宝典》——在被企业拒绝后是否可以再申请?