QCoreApplication Class
The QCoreApplication class provides an event loop for Qt applications without UI. More...
| Header: | #include <QCoreApplication> |
| qmake: | QT += core |
| Inherits: | QObject |
| Inherited By: |
Properties
|
|
Signals
| void | applicationNameChanged() |
| void | applicationVersionChanged() |
| void | organizationDomainChanged() |
| void | organizationNameChanged() |
Static Public Members
| QString | applicationName() |
| QString | applicationVersion() |
| bool | isQuitLockEnabled() |
| QString | organizationDomain() |
| QString | organizationName() |
| void | setApplicationName(const QString &application) |
| void | setApplicationVersion(const QString &version) |
| void | setOrganizationDomain(const QString &orgDomain) |
| void | setOrganizationName(const QString &orgName) |
| void | setQuitLockEnabled(bool enabled) |
Macros
| Q_COREAPP_STARTUP_FUNCTION(QtStartUpFunction ptr) | |
| Q_DECLARE_TR_FUNCTIONS(context) |
Detailed Description
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. For applications that use the Qt Widgets module, see 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.
The Event Loop and Event Handling
The event loop is started with a call to exec(). Long-running operations can call processEvents() to keep the application responsive.
In general, we recommend that you create a QCoreApplication, QGuiApplication or a QApplication object in your main() function as early as possible. exec() will not return until the event loop exits; e.g., when quit() is called.
Several static convenience functions are also provided. The QCoreApplication object is available from instance(). Events can be sent with sendEvent() or posted to an event queue with postEvent(). Pending events can be removed with removePostedEvents() or dispatched with sendPostedEvents().
The class provides a quit() slot and an aboutToQuit() signal.
Application and Library Paths
An application has an applicationDirPath() and an applicationFilePath(). Library paths (see QLibrary) can be retrieved with libraryPaths() and manipulated by setLibraryPaths(), addLibraryPath(), and removeLibraryPath().
Internationalization and Translations
Translation files can be added or removed using installTranslator() and removeTranslator(). Application strings can be translated using translate(). The QObject::tr() and QObject::trUtf8() functions are implemented in terms of translate().
Accessing Command Line Arguments
The command line arguments which are passed to QCoreApplication's constructor should be accessed using the arguments() function.
Note: QCoreApplication removes option -qmljsdebugger="...". It parses the argument of qmljsdebugger, and then removes this option plus its argument.
For more advanced command line option handling, create a QCommandLineParser.
Locale Settings
On Unix/Linux Qt is configured to use the system locale settings by default. This can cause a conflict when using POSIX functions, for instance, when converting between data types such as floats and strings, since the notation may differ between locales. To get around this problem, call the POSIX function setlocale(LC_NUMERIC,"C") right after initializing QApplication, QGuiApplication or QCoreApplication to reset the locale that is used for number formatting to "C"-locale.
See also QGuiApplication, QAbstractEventDispatcher, QEventLoop, Semaphores Example, and Wait Conditions Example.
Property Documentation
applicationName : QString
This property holds the name of this application
The value is used by the QSettings class when it is constructed using the empty constructor. This saves having to repeat this information each time a QSettings object is created.
If not set, the application name defaults to the executable name (since 5.0).
Access functions:
| QString | applicationName() |
| void | setApplicationName(const QString &application) |
Notifier signal:
| void | applicationNameChanged() |
See also organizationName, organizationDomain, applicationVersion, and applicationFilePath().
applicationVersion : QString
This property holds the version of this application
If not set, the application version defaults to a platform-specific value determined from the main application executable or package (since Qt 5.9):
| Platform | Source |
|---|---|
| Windows (classic desktop) | PRODUCTVERSION parameter of the VERSIONINFO resource |
| Universal Windows Platform | version attribute of the application package manifest |
| macOS, iOS, tvOS, watchOS | CFBundleVersion property of the information property list |
| Android | android:versionName property of the AndroidManifest.xml manifest element |
On other platforms, the default is the empty string.
This property was introduced in Qt 4.4.
Access functions:
| QString | applicationVersion() |
| void | setApplicationVersion(const QString &version) |
Notifier signal:
| void | applicationVersionChanged() |
See also applicationName, organizationName, and organizationDomain.
organizationDomain : QString
This property holds the Internet domain of the organization that wrote this application
The value is used by the QSettings class when it is constructed using the empty constructor. This saves having to repeat this information each time a QSettings object is created.
On Mac, QSettings uses organizationDomain() as the organization if it's not an empty string; otherwise it uses organizationName(). On all other platforms, QSettings uses organizationName() as the organization.
Access functions:
| QString | organizationDomain() |
| void | setOrganizationDomain(const QString &orgDomain) |
Notifier signal:
| void | organizationDomainChanged() |
See also organizationName, applicationName, and applicationVersion.
organizationName : QString
This property holds the name of the organization that wrote this application
The value is used by the QSettings class when it is constructed using the empty constructor. This saves having to repeat this information each time a QSettings object is created.
On Mac, QSettings uses organizationDomain() as the organization if it's not an empty string; otherwise it uses organizationName(). On all other platforms, QSettings uses organizationName() as the organization.
Access functions:
| QString | organizationName() |
| void | setOrganizationName(const QString &orgName) |
Notifier signal:
| void | organizationNameChanged() |
See also organizationDomain and applicationName.
quitLockEnabled : bool
This property holds whether the use of the QEventLoopLocker feature can cause the application to quit.
The default is true.
Access functions:
| bool | isQuitLockEnabled() |
| void | setQuitLockEnabled(bool enabled) |
See also QEventLoopLocker.
Macro Documentation
Q_COREAPP_STARTUP_FUNCTION(QtStartUpFunction ptr)
Adds a global function that will be called from the 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.
The function specified by ptr should take no arguments and should return nothing. For example:
// 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.
If QCoreApplication is deleted and another QCoreApplication is created, the startup function will be invoked again.
Note: 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.
Note: This function is reentrant.
This function was introduced in 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 = nullptr); static inline QString trUtf8(const char *sourceText, const char *comment = nullptr);
This macro is useful if you want to use QObject::tr() or QObject::trUtf8() in classes that don't inherit from QObject.
Q_DECLARE_TR_FUNCTIONS() must appear at the very top of the class definition (before the first public: or protected:). For example:
class MyMfcView : public CView { Q_DECLARE_TR_FUNCTIONS(MyMfcView) public: MyMfcView(); ... };
The context parameter is normally the class name, but it can be any text.
See also Q_OBJECT, QObject::tr(), and QObject::trUtf8().