QCoreApplication Class
The QCoreApplication class provides an event loop for console applications. More…
| Header | #include <QCoreApplication> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherits | QObject |
| Inherited by | QGuiApplication |
List of all members, including inherited members
Public Functions
| QCoreApplication(int &argc, char **argv) | |
| ~QCoreApplication() | |
| virtual bool | notify(QObject *receiver, QEvent *event) |
Public Slots
| void | quit() |
Signals
| void | aboutToQuit() |
Static Public Members
Detailed Description
Create one QCoreApplication in main() before using timers, posted events or queued connections, and call exec() to run the event loop:
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QTimer::singleShot(100, [] { QCoreApplication::quit(); });
return app.exec();
}
The class also holds the command line, the application name and version, and provides functions to send and post events.
See also QEventLoop, QObject and QCommandLineParser.
Differences from Qt
Only part of the Qt API is present: the library paths are only a list, as Nt loads no plugins, and there is no postEvent() priority. Creating a second application object aborts the process. exit() and quit() stop every running QEventLoop of the thread that created the application object, and aboutToQuit() is emitted after the main loop has stopped. Unlike Qt, they do not make later calls to exec() return at once. postEvent() may be called from any thread; processEvents() and sendPostedEvents() work on the calling thread's queue. qApp is defined as QCoreApplication::instance().
Member Function Documentation
QCoreApplication::QCoreApplication(int &argc, char **argv)
Constructs a core application from the command line given by argc and argv. Both must remain valid for the lifetime of the application, and only one application object may exist at a time; creating a second one prints a message and aborts the process.
See also arguments().
QCoreApplication::~QCoreApplication()
Destroys the application object. After this, instance() returns nullptr.
void QCoreApplication::aboutToQuit()
This signal is emitted by exec() after the event loop has stopped, just before exec() returns. Use it to release resources.
See also quit().
void QCoreApplication::addLibraryPath(const QString &path)
Puts path at the front of the library paths, so it is searched first. Nothing changes if path is empty or already in the list.
See also removeLibraryPath() and libraryPaths().
QString QCoreApplication::applicationDirPath()
Returns the directory that contains the executable file of the application.
This function was introduced in 4.0.
See also applicationFilePath().
QString QCoreApplication::applicationFilePath()
Returns the absolute path of the executable file of the application, with symbolic links resolved. On Linux it is read from /proc/self/exe; elsewhere it is derived from the first command-line argument.
This function was introduced in 4.0.
See also applicationDirPath() and arguments().
QString QCoreApplication::applicationName()
Returns the application name. Unless it was set with setApplicationName(), this is the file name part of the program path in the command line, or an empty string if there is no command line.
See also setApplicationName() and arguments().
qint64 QCoreApplication::applicationPid()
Returns the process identifier of the application.
This function was introduced in 4.4.
QString QCoreApplication::applicationVersion()
Returns the application version, which is empty unless it was set with setApplicationVersion().
See also setApplicationVersion().
QStringList QCoreApplication::arguments()
Returns the command line as passed to the constructor, decoded as UTF-8. The first entry is the program name.
See also applicationName().
bool QCoreApplication::closingDown()
Returns true if the application object is being destroyed or has been destroyed; otherwise returns false. Creating a new application object makes this false again.
See also startingUp().
QAbstractEventDispatcher * QCoreApplication::eventDispatcher()
Returns the event dispatcher of the thread that the application object lives in, which is the main thread, or nullptr if there is no application object.
See also QAbstractEventDispatcher::instance() and QThread::eventDispatcher().
int QCoreApplication::exec()
Enters the main event loop and waits until exit() or quit() is called, then emits aboutToQuit() and returns the code passed to exit().
See also quit(), exit() and QEventLoop::exec().
void QCoreApplication::exit(int returnCode = 0)
Tells every running event loop to exit with the return code returnCode.
bool QCoreApplication::installTranslator(QTranslator *translationFile)
Adds translationFile to the translators that tr() and translate() use, and returns true. Translators installed later are searched first. A QEvent::LanguageChange event is posted to the application object. Returns false if translationFile is nullptr or empty.
See also removeTranslator() and translate().
QCoreApplication * QCoreApplication::instance()
Returns the application object, or nullptr if none exists.
bool QCoreApplication::instanceExists()
Returns true if an application object exists; otherwise returns false. Unlike instance(), this is also false while the application object is being destroyed.
This function was introduced in 6.10.
See also instance() and closingDown().
bool QCoreApplication::isQuitLockEnabled()
Returns true if the application quits when the last QEventLoopLocker created without a target is destroyed.
See also setQuitLockEnabled().
QStringList QCoreApplication::libraryPaths()
Returns the directories searched for plugins and libraries. Unless setLibraryPaths() was called, the list holds the directory of the executable. Nt loads no plugins, so the list is only kept for applications that use it themselves.
See also setLibraryPaths() and addLibraryPath().
bool QCoreApplication::notify(QObject *receiver, QEvent *event)
Delivers event to receiver by calling QObject::event() and returns its result. Reimplement it to intercept every event sent to any object in the application.
See also sendEvent() and QObject::event().
QString QCoreApplication::organizationDomain()
Returns the Internet domain of the organization, which is empty unless it was set with setOrganizationDomain().
See also setOrganizationDomain() and organizationName().
QString QCoreApplication::organizationName()
Returns the name of the organization, which is empty unless it was set with setOrganizationName().
See also setOrganizationName() and organizationDomain().
void QCoreApplication::postEvent(QObject *receiver, QEvent *event)
Adds event to the event queue of receiver and returns immediately. The event is delivered from the event loop. The queue takes ownership of event and deletes it after delivery; if receiver is nullptr the event is deleted at once.
See also sendEvent() and removePostedEvents().
void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents)
Processes pending events once according to flags. With QEventLoop::WaitForMoreEvents the call blocks until an event is available. Other flags are ignored.
See also QEventLoop::processEvents().
void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags, int ms)
This is an overloaded function.
Processes the pending events of the calling thread for at most ms milliseconds, or until there are none left, as flags selects. It does not wait for new events.
void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags, QDeadlineTimer deadline)
This is an overloaded function.
Processes the pending events of the calling thread until deadline expires or there are none left. flags is accepted for compatibility.
This function was introduced in 6.7.
void QCoreApplication::quit()
Tells the application to exit with return code 0. This is the same as calling exit(0).
See also exit() and aboutToQuit().
void QCoreApplication::removeLibraryPath(const QString &path)
Removes path from the library paths.
See also addLibraryPath().
void QCoreApplication::removePostedEvents(QObject *receiver, int eventType = 0)
Removes the posted events for receiver that have not been delivered yet. If eventType is 0, every pending event is removed; otherwise only events of that type.
See also postEvent().
bool QCoreApplication::removeTranslator(QTranslator *translationFile)
Removes translationFile from the installed translators and returns true, or returns false if it was not installed. A QEvent::LanguageChange event is posted to the application object.
See also installTranslator().
bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event)
Sends event directly to receiver through notify() and returns the value the event handler returned. The caller keeps ownership of event. Returns false if receiver or event is nullptr.
See also postEvent() and notify().
void QCoreApplication::sendPostedEvents(QObject *receiver = nullptr, int eventType = 0)
Delivers the events posted to receiver that match eventType right away, in the calling thread, and removes them from the queue. If receiver is nullptr, the events of every object that lives in the calling thread match. An eventType of 0 matches all events except QEvent::DeferredDelete, which is delivered only when asked for by type or by an event loop. receiver must live in the calling thread.
See also postEvent() and processEvents().
void QCoreApplication::setApplicationName(const QString &name)
Sets the application name to name.
See also applicationName().
void QCoreApplication::setApplicationVersion(const QString &version)
Sets the application version to version.
See also applicationVersion() and QCommandLineParser::addVersionOption().
void QCoreApplication::setLibraryPaths(const QStringList &paths)
Replaces the library paths with paths.
See also libraryPaths().
void QCoreApplication::setOrganizationDomain(const QString &orgDomain)
Sets the Internet domain of the organization to orgDomain.
See also organizationDomain() and setOrganizationName().
void QCoreApplication::setOrganizationName(const QString &orgName)
Sets the name of the organization to orgName. QSettings uses it to locate the settings of the application.
See also organizationName(), setOrganizationDomain() and QSettings.
void QCoreApplication::setQuitLockEnabled(bool enabled)
Sets whether the application quits when the last QEventLoopLocker created without a target is destroyed to enabled. The default is true.
See also isQuitLockEnabled() and QEventLoopLocker.
bool QCoreApplication::startingUp()
Returns true if no application object has been created yet; otherwise returns false.
See also closingDown().
QString QCoreApplication::translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1)
Returns the translation of the UTF-8 text sourceText in context, with the disambiguation comment disambiguation, from the installed translators, most recently installed first. If none has a translation, sourceText is returned. If n is not negative, the plural form for n is used and every %n in the result is replaced by n.
This function is thread-safe.
See also installTranslator().