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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM