QCoreApplication Class▲
-
Header: QCoreApplication
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
-
Inherits: QObject
-
Inherited By: QAndroidService
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() function is 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.
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▲
See also QGuiApplication, QAbstractEventDispatcher, QEventLoop, Semaphores Example, 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:
-
applicationName()
-
void setApplicationName(const &application)
Notifier signal:
-
void applicationNameChanged()
See Also▲
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 |
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.
Access functions:
-
applicationVersion()
-
void setApplicationVersion(const &version)
Notifier signal:
-
void applicationVersionChanged()
See Also▲
See also applicationName, organizationName, 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:
-
organizationDomain()
-
void setOrganizationDomain(const &orgDomain)
Notifier signal:
-
void organizationDomainChanged()
See Also▲
See also organizationName, applicationName, 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:
-
organizationName()
-
void setOrganizationName(const &orgName)
Notifier signal:
-
void organizationNameChanged()
See Also▲
See also organizationDomain, 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▲
See also QEventLoopLocker
Member Function Documentation▲
QCoreApplication::QCoreApplication(int &argc, char **argv)▲
Constructs a Qt core application. Core applications are applications without a graphical user interface. Such applications are used at the console or as server processes.
The argc and argv arguments are processed by the application, and made available in a more convenient form by the arguments() function.
The data referred to by argc and argv must stay valid for the entire lifetime of the QCoreApplication object. In addition, argc must be greater than zero and argv must contain at least one valid character string.
[virtual] QCoreApplication::~QCoreApplication()▲
Destroys the QCoreApplication object.
void QCoreApplication::aboutToQuit()▲
This signal is emitted when the application is about to quit the main event loop, e.g. when the event loop level drops to zero. This may happen either after a call to quit() from inside the application or when the user shuts down the entire desktop session.
The signal is particularly useful if your application has to do some last-second cleanup. Note that no user interaction is possible in this state.
This is a private signal. It can be used in signal connections but cannot be emitted by the user.
See Also▲
See also quit()
[static] void QCoreApplication::addLibraryPath(const QString &path)▲
Prepends path to the beginning of the library path list, ensuring that it is searched for libraries first. If path is empty or already in the path list, the path list is not changed.
The default path list consists of one or two entries. The first is the installation directory for plugins, which is INSTALL/plugins, where INSTALL is the directory where Qt was installed. The second is the application's own directory (not the current directory), but only after the QCoreApplication object is instantiated.
The library paths are reset to the default when an instance of QCoreApplication is destructed.
See Also▲
See also removeLibraryPath(), libraryPaths(), setLibraryPaths()
[static] QString QCoreApplication::applicationDirPath()▲
Returns the directory that contains the application executable.
For example, if you have installed Qt in the C:\Qt directory, and you run the regexp example, this function will return "C:/Qt/examples/tools/regexp".
On macOS and iOS this will point to the directory actually containing the executable, which may be inside an application bundle (if the application is bundled).
On Linux, this function will try to get the path from the /proc file system. If that fails, it assumes that argv[0] contains the absolute file name of the executable. The function also assumes that the current directory has not been changed by the application.
See Also▲
See also applicationFilePath()
[static] QString QCoreApplication::applicationFilePath()▲
Returns the file path of the application executable.
For example, if you have installed Qt in the /usr/local/qt directory, and you run the regexp example, this function will return "/usr/local/qt/examples/tools/regexp/regexp".
On Linux, this function will try to get the path from the /proc file system. If that fails, it assumes that argv[0] contains the absolute file name of the executable. The function also assumes that the current directory has not been changed by the application.
See Also▲
See also applicationDirPath()
[static] qint64 QCoreApplication::applicationPid()▲
Returns the current process ID for the application.
[static] QStringList QCoreApplication::arguments()▲
Returns the list of command-line arguments.
Usually arguments().at(0) is the program name, arguments().at(1) is the first argument, and arguments().last() is the last argument. See the note below about Windows.
Calling this function is slow - you should store the result in a variable when parsing the command line.
On Unix, this list is built from the argc and argv parameters passed to the constructor in the main() function. The string-data in argv is interpreted using QString::fromLocal8Bit(); hence it is not possible to pass, for example, Japanese command line arguments on a system that runs in a Latin1 locale. Most modern Unix systems do not have this limitation, as they are Unicode-based.
On Windows, the list is built from the argc and argv parameters only if modified argv/argc parameters are passed to the constructor. In that case, encoding problems might occur.
Otherwise, the arguments() are constructed from the return value of GetCommandLine(). As a result of this, the string given by arguments().at(0) might not be the program name on Windows, depending on how the application was started.
See Also▲
See also applicationFilePath(), QCommandLineParser
[static] bool QCoreApplication::closingDown()▲
Returns true if the application objects are being destroyed; otherwise returns false.
See Also▲
See also startingUp()
[override virtual protected] bool QCoreApplication::event(QEvent *e)▲
Reimplements: QObject::event(QEvent *e).
[static] QAbstractEventDispatcher *QCoreApplication::eventDispatcher()▲
Returns a pointer to the event dispatcher object for the main thread. If no event dispatcher exists for the thread, this function returns nullptr.