QtBase
v6.3.1
|
The QCoreApplication class provides an event loop for Qt applications without UI. More...
#include <qcoreapplication.h>
Public Types | |
enum | { ApplicationFlags = QT_VERSION } |
Public Slots | |
static void | quit () |
static void | exit (int retcode=0) |
Public Slots inherited from QObject | |
void | deleteLater () |
Signals | |
void | aboutToQuit (QPrivateSignal) |
void | organizationNameChanged () |
void | organizationDomainChanged () |
void | applicationNameChanged () |
void | applicationVersionChanged () |
Signals inherited from QObject | |
void | destroyed (QObject *=nullptr) |
void | objectNameChanged (const QString &objectName, QPrivateSignal) |
Protected Member Functions | |
bool | event (QEvent *) override |
virtual bool | compressEvent (QEvent *, QObject *receiver, QPostEventList *) |
QCoreApplication (QCoreApplicationPrivate &p) | |
Protected Member Functions inherited from QObject | |
QObject * | sender () const |
int | senderSignalIndex () const |
int | receivers (const char *signal) const |
bool | isSignalConnected (const QMetaMethod &signal) const |
virtual void | timerEvent (QTimerEvent *event) |
virtual void | childEvent (QChildEvent *event) |
virtual void | customEvent (QEvent *event) |
virtual void | connectNotify (const QMetaMethod &signal) |
virtual void | disconnectNotify (const QMetaMethod &signal) |
QObject (QObjectPrivate &dd, QObject *parent=nullptr) | |
Properties | |
QString | applicationName |
the name of this application More... | |
QString | applicationVersion |
the version of this application More... | |
QString | organizationName |
the name of the organization that wrote this application More... | |
QString | organizationDomain |
the Internet domain of the organization that wrote this application More... | |
bool | quitLockEnabled |
Whether the use of the QEventLoopLocker feature can cause the application to quit. More... | |
Properties inherited from QObject | |
QString | objectName |
the name of this object More... | |
Friends | |
class | QApplication |
class | QApplicationPrivate |
class | QGuiApplication |
class | QGuiApplicationPrivate |
class | QWidget |
class | QWidgetWindow |
class | QWidgetPrivate |
class | QEventDispatcherUNIXPrivate |
class | QCocoaEventDispatcherPrivate |
class | QCommandLineParserPrivate |
bool | qt_sendSpontaneousEvent (QObject *, QEvent *) |
Q_CORE_EXPORT QString | qAppName () |
Related Functions | |
(Note that these are not member functions.) | |
void | qAddPostRoutine (QtCleanUpFunction ptr) |
void | qRemovePostRoutine (QtCleanUpFunction ptr) |
void * | resolveInterface (const char *name, int revision) const |
Related Functions inherited from QObject | |
template< class T > T | qobject_cast (const QObject *object) |
template< typename T > T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
template< typename T > QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QObjectList | |
Additional Inherited Members | |
Protected Attributes inherited from QObject | |
QScopedPointer< QObjectData > | d_ptr |
The QCoreApplication class provides an event loop for Qt applications without UI.
\inmodule QtCore
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.
Definition at line 74 of file qcoreapplication.h.
anonymous enum |
Enumerator | |
---|---|
ApplicationFlags |
Definition at line 94 of file qcoreapplication.h.
QCoreApplication::QCoreApplication | ( | int & | argc, |
char ** | argv, | ||
int | _internal = ApplicationFlags |
||
) |
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.
Definition at line 749 of file qcoreapplication.cpp.
QCoreApplication::~QCoreApplication | ( | ) |
Destroys the QCoreApplication object.
Definition at line 882 of file qcoreapplication.cpp.
|
protected |
Definition at line 723 of file qcoreapplication.cpp.
|
signal |
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.
|
static |
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).
{/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.Definition at line 2328 of file qcoreapplication.cpp.
|
static |
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".
{/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.Definition at line 2380 of file qcoreapplication.cpp.
|
static |
|
signal |
|
static |
Returns the current process ID for the application.
Definition at line 2439 of file qcoreapplication.cpp.
|
static |
|
signal |
|
static |
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 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 \l{https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getcommandlinea}{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.
Definition at line 2481 of file qcoreapplication.cpp.
|
static |
Returns true
if the application objects are being destroyed; otherwise returns false
.
Definition at line 1257 of file qcoreapplication.cpp.
|
protectedvirtual |
Reimplemented in QApplication, and QGuiApplication.
Definition at line 1633 of file qcoreapplication.cpp.
|
overrideprotectedvirtual |
\reimp
Reimplemented from QObject.
Reimplemented in MyApplication, MyApplication, QApplication, and QGuiApplication.
Definition at line 1970 of file qcoreapplication.cpp.
|
static |
Returns a pointer to the event dispatcher object for the main thread. If no event dispatcher exists for the thread, this function returns \nullptr.
Definition at line 2997 of file qcoreapplication.cpp.
|
static |
Enters the main event loop and waits until exit() is called. Returns the value that was passed to exit() (which is 0 if exit() is called via quit()).
It is necessary to call this function to start event handling. The main event loop receives events from the window system and dispatches these to the application widgets.
To make your application perform idle processing (by executing a special function whenever there are no pending events), use a QTimer with 0 timeout. More advanced idle processing schemes can be achieved using processEvents().
We recommend that you connect clean-up code to the \l{QCoreApplication::}{aboutToQuit()} signal, instead of putting it in your application's {main()} function because on some platforms the exec() call may not return. For example, on Windows when the user logs off, the system terminates the process after Qt closes all top-level windows. Hence, there is no guarantee that the application will have time to exit its event loop and execute code at the end of the
{main()} function after the exec() call.
Definition at line 1369 of file qcoreapplication.cpp.
|
staticslot |
Tells the application to exit with a return code.
After this function has been called, the application leaves the main event loop and returns from the call to exec(). The exec() function returns returnCode. If the event loop is not running, this function does nothing.
By convention, a returnCode of 0 means success, and any non-zero value indicates an error.
It's good practice to always connect signals to this slot using a \l{Qt::}{QueuedConnection}. If a signal connected (non-queued) to this slot is emitted before control enters the main event loop (such as before "int main" calls \l{QCoreApplication::}{exec()}), the slot has no effect and the application never exits. Using a queued connection ensures that the slot will not be invoked until after control enters the main event loop.
Note that unlike the C library function of the same name, this function does return to the caller – it is event processing that stops.
Note also that this function is not thread-safe. It should be called only from the main thread (the thread that the QCoreApplication object is processing events on). To ask the application to exit from another thread, either use QCoreApplication::quit() or instead call this function from the main thread with QMetaMethod::invokeMethod().
Definition at line 1444 of file qcoreapplication.cpp.
void QCoreApplication::installNativeEventFilter | ( | QAbstractNativeEventFilter * | filterObj | ) |
Installs an event filter filterObj for all native events received by the application in the main thread.
The event filter filterObj receives events via its \l {QAbstractNativeEventFilter::}{nativeEventFilter()} function, which is called for all native events received in the main thread.
The QAbstractNativeEventFilter::nativeEventFilter() function should return true if the event should be filtered, i.e. stopped. It should return false to allow normal Qt processing to continue: the native event can then be translated into a QEvent and handled by the standard Qt \l{QEvent} {event} filtering, e.g. QObject::installEventFilter().
If multiple event filters are installed, the filter that was installed last is activated first.
For maximum portability, you should always try to use QEvent and QObject::installEventFilter() whenever possible.
Definition at line 2959 of file qcoreapplication.cpp.
|
static |
Adds the translation file translationFile to the list of translation files to be used for translations.
Multiple translation files can be installed. Translations are searched for in the reverse order in which they were installed, so the most recently installed translation file is searched first and the first translation file installed is searched last. The search stops as soon as a translation containing a matching string is found.
Installing or removing a QTranslator, or changing an installed QTranslator generates a \l{QEvent::LanguageChange}{LanguageChange} event for the QCoreApplication instance. A QApplication instance will propagate the event to all toplevel widgets, where a reimplementation of changeEvent can re-translate the user interface by passing user-visible strings via the tr() function to the respective property setters. User-interface classes generated by Qt Designer provide a retranslateUi()
function that can be called.
The function returns true
on success and false on failure.
Definition at line 2118 of file qcoreapplication.cpp.
|
inlinestatic |
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
If no instance has been allocated, \nullptr is returned.
Definition at line 122 of file qcoreapplication.h.
|
static |
Definition at line 1025 of file qcoreapplication.cpp.
|
static |
Returns true if the application is allowed to run setuid on UNIX platforms.
Definition at line 954 of file qcoreapplication.cpp.
Sends event to receiver: {receiver}->event(event). Returns the value that is returned from the receiver's event handler. Note that this function is called for all events sent to any object in any thread.
For certain types of events (e.g. mouse and key events), the event will be propagated to the receiver's parent and so on up to the top-level object if the receiver is not interested in the event (i.e., it returns false
).
There are five different ways that events can be processed; reimplementing this virtual function is just one of them. All five approaches are listed below: \list 1
{Future direction:} This function will not be called for objects that live outside the main thread in Qt 6. Applications that need that functionality should find other solutions for their event inspection needs in the meantime. The change may be extended to the main thread, causing this function to be deprecated.
Reimplemented in QApplication, and QGuiApplication.
Definition at line 1138 of file qcoreapplication.cpp.
|
static |
|
signal |
|
static |
|
signal |
|
static |
Adds the event event, with the object receiver as the receiver of the event, to an event queue and returns immediately.
The event must be allocated on the heap since the post event queue will take ownership of the event and delete it once it has been posted. It is {not safe} to access the event after it has been posted.
When control returns to the main event loop, all events that are stored in the queue will be sent using the notify() function.
Events are sorted in descending priority order, i.e. events with a high priority are queued before events with a lower priority. The priority can be any integer value, i.e. between INT_MAX and INT_MIN, inclusive; see Qt::EventPriority for more details. Events with equal priority will be processed in the order posted.
\threadsafe
Definition at line 1559 of file qcoreapplication.cpp.
|
static |
|
static |
Processes some pending events for the calling thread according to the specified flags.
Use of this function is discouraged. Instead, prefer to move long operations out of the GUI thread into an auxiliary one and to completely avoid nested event loop processing. If event processing is really necessary, consider using \l QEventLoop instead.
In the event that you are running a local loop which calls this function continuously, without an event loop, the \l{QEvent::DeferredDelete}{DeferredDelete} events will not be processed. This can affect the behaviour of widgets, e.g. QToolTip, that rely on \l{QEvent::DeferredDelete}{DeferredDelete} events to function properly. An alternative would be to call \l{QCoreApplication::sendPostedEvents()}{sendPostedEvents()} from within that local loop.
Calling this function processes events only for the calling thread, and returns after all available events have been processed. Available events are events queued before the function call. This means that events that are posted while the function runs will be queued until a later round of event processing.
\threadsafe
Definition at line 1291 of file qcoreapplication.cpp.
|
staticslot |
\threadsafe
Asks the application to quit.
The request may be ignored if the application prevents the quit, for example if one of its windows can't be closed. The application can affect this by handling the QEvent::Quit event on the application level, or QEvent::Close events for the individual windows.
If the quit is not interrupted the application will exit with return code 0 (success).
To exit the application without a chance of being interrupted, call exit() directly. Note that method is not thread-safe.
It's good practice to always connect signals to this slot using a \l{Qt::}{QueuedConnection}. If a signal connected (non-queued) to this slot is emitted before control enters the main event loop (such as before "int main" calls \l{QCoreApplication::}{exec()}), the slot has no effect and the application never exits. Using a queued connection ensures that the slot will not be invoked until after control enters the main event loop.
Example:
{Thread-safety note}: this function may be called from any thread to thread-safely cause the currently-running main application loop to exit. However, thread-safety is not guaranteed if the QCoreApplication object is being destroyed at the same time.
Definition at line 2050 of file qcoreapplication.cpp.
void QCoreApplication::removeNativeEventFilter | ( | QAbstractNativeEventFilter * | filterObject | ) |
Removes an event filterObject from this object. The request is ignored if such an event filter has not been installed.
All event filters for this object are automatically removed when this object is destroyed.
It is always safe to remove an event filter, even during event filter activation (i.e. from the nativeEventFilter() function).
Definition at line 2985 of file qcoreapplication.cpp.
Removes all events of the given eventType that were posted using postEvent() for receiver.
The events are not dispatched, instead they are removed from the queue. You should never need to call this function. If you do call it, be aware that killing events may cause receiver to break one or more invariants.
If receiver is \nullptr, the events of eventType are removed for all objects. If eventType is 0, all the events are removed for receiver. You should never call this function with eventType of 0.
\threadsafe
Definition at line 1874 of file qcoreapplication.cpp.
|
static |
Removes the translation file translationFile from the list of translation files used by this application. (It does not delete the translation file from the file system.)
The function returns true
on success and false on failure.
Definition at line 2154 of file qcoreapplication.cpp.
Sends event event directly to receiver receiver, using the notify() function. Returns the value that was returned from the event handler.
The event is not deleted when the event has been sent. The normal approach is to create the event on the stack, for example:
Definition at line 1475 of file qcoreapplication.cpp.
Immediately dispatches all events which have been previously queued with QCoreApplication::postEvent() and which are for the object receiver and have the event type event_type.
Events from the window system are not dispatched by this function, but by processEvents().
If receiver is \nullptr, the events of event_type are sent for all objects. If event_type is 0, all the events are sent for receiver.
Definition at line 1697 of file qcoreapplication.cpp.
Definition at line 2628 of file qcoreapplication.cpp.
Definition at line 2681 of file qcoreapplication.cpp.
|
static |
[95]
Sets the attribute attribute if on is true; otherwise clears the attribute.
[96]
Definition at line 970 of file qcoreapplication.cpp.
|
static |
Sets the event dispatcher for the main thread to eventDispatcher. This is only possible as long as there is no event dispatcher installed yet. That is, before QCoreApplication has been instantiated. This method takes ownership of the object.
Definition at line 3010 of file qcoreapplication.cpp.
[11]
[12]
Definition at line 2557 of file qcoreapplication.cpp.
|
static |
Definition at line 1032 of file qcoreapplication.cpp.
|
static |
Allows the application to run setuid on UNIX platforms if allow is true.
If allow is false (the default) and Qt detects the application is running with an effective user id different than the real user id, the application will be aborted when a QCoreApplication instance is created.
Qt is not an appropriate solution for setuid programs due to its large attack surface. However some applications may be required to run in this manner for historical reasons. This flag will prevent Qt from aborting the application when this is detected, and must be set before a QCoreApplication instance is created.
Definition at line 941 of file qcoreapplication.cpp.
|
static |
Returns true
if an application object has not been created yet; otherwise returns false
.
Definition at line 1245 of file qcoreapplication.cpp.
|
static |
Returns true
if attribute attribute is set; otherwise returns false
.
Definition at line 1007 of file qcoreapplication.cpp.
|
static |
\threadsafe
Returns the translation text for sourceText, by querying the installed translation files. The translation files are searched from the most recently installed file back to the first installed file.
QObject::tr() provides this functionality more conveniently.
context is typically a class name (e.g., "MyDialog") and sourceText is either English text or a short identifying text.
disambiguation is an identifying string, for when the same sourceText is used in different roles within the same context. By default, it is \nullptr.
See the \l QTranslator and \l QObject::tr() documentation for more information about contexts, disambiguations and comments.
n is used in conjunction with n
to support plural forms. See QObject::tr() for details.
If none of the translation files contain a translation for sourceText in context, this function returns a QString equivalent of sourceText.
This function is not virtual. You can use alternative translation techniques by subclassing \l QTranslator.
Definition at line 2235 of file qcoreapplication.cpp.
|
related |
\macro Q_COREAPP_STARTUP_FUNCTION(QtStartUpFunction ptr)
\reentrant
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:
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.
\threadsafe
Adds a global routine that will be called from the QCoreApplication destructor. This function is normally used to add cleanup routines for program-wide functionality.
The cleanup routines are called in the reverse order of their addition.
The function specified by ptr should take no arguments and should return nothing. For example:
Note that for an application- or module-wide cleanup, qAddPostRoutine() is often not suitable. For example, if the program is split into dynamically loaded modules, the relevant module may be unloaded long before the QCoreApplication destructor is called. In such cases, if using qAddPostRoutine() is still desirable, qRemovePostRoutine() can be used to prevent a routine from being called by the QCoreApplication destructor. For example, if that routine was called before the module was unloaded.
For modules and libraries, using a reference-counted initialization manager or Qt's parent-child deletion mechanism may be better. Here is an example of a private class that uses the parent-child mechanism to call a cleanup function at the right time:
By selecting the right parent object, this can often be made to clean up the module's data at the right moment.
Definition at line 292 of file qcoreapplication.cpp.
|
friend |
Definition at line 211 of file qcoreapplication.h.
|
friend |
Definition at line 212 of file qcoreapplication.h.
|
friend |
Definition at line 582 of file qcoreapplication.cpp.
|
friend |
Definition at line 220 of file qcoreapplication.h.
|
friend |
Definition at line 224 of file qcoreapplication.h.
|
friend |
Definition at line 219 of file qcoreapplication.h.
|
friend |
Definition at line 213 of file qcoreapplication.h.
|
friend |
Definition at line 214 of file qcoreapplication.h.
|
related |
\threadsafe
Removes the cleanup routine specified by ptr from the list of routines called by the QCoreApplication destructor. The routine must have been previously added to the list by a call to qAddPostRoutine(), otherwise this function has no effect.
Definition at line 301 of file qcoreapplication.cpp.
|
friend |
Definition at line 215 of file qcoreapplication.h.
|
friend |
Definition at line 217 of file qcoreapplication.h.
|
friend |
Definition at line 216 of file qcoreapplication.h.
\macro Q_DECLARE_TR_FUNCTIONS(context)
The Q_DECLARE_TR_FUNCTIONS() macro declares and implements the translation function tr()
with this signature:
This macro is useful if you want to use QObject::tr() 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:
The context parameter is normally the class name, but it can be any text.
Definition at line 3129 of file qcoreapplication.cpp.
|
readwrite |
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).
Definition at line 254 of file qcoreapplication.h.
|
readwrite |
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):
\table \header
On other platforms, the default is the empty string.
Definition at line 254 of file qcoreapplication.h.
|
readwrite |
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.
Definition at line 254 of file qcoreapplication.h.
|
readwrite |
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 \l {QCoreApplication::}{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.
Definition at line 254 of file qcoreapplication.h.
|
readwrite |
Whether the use of the QEventLoopLocker feature can cause the application to quit.
The default is true
.
Definition at line 254 of file qcoreapplication.h.