CopperSpice API
1.9.2
|
Implements an event loop for console applications. More...
Public Signals | |
void | aboutToQuit () |
void | unixSignal (int signalId) |
Public Signals inherited from QObject | |
void | destroyed (QObject *obj=nullptr) |
void | objectNameChanged (const QString &objectName) |
Public Slots | |
static void | quit () |
Public Slots inherited from QObject | |
void | deleteLater () |
Public Methods | |
QCoreApplication (int &argc, char **argv) | |
~QCoreApplication () | |
void | installNativeEventFilter (QAbstractNativeEventFilter *filterObj) |
virtual bool | notify (QObject *receiver, QEvent *event) |
void | removeNativeEventFilter (QAbstractNativeEventFilter *filterObj) |
Public Methods inherited from QObject | |
QObject (QObject *parent=nullptr) | |
~QObject () | |
bool | blockSignals (bool block) |
const QList< QObject * > & | children () const |
bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
bool | connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
bool | disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const |
bool | disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
bool | disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const |
void | dumpObjectInfo () |
void | dumpObjectTree () |
QList< QString > | dynamicPropertyNames () const |
virtual bool | eventFilter (QObject *watched, QEvent *event) |
template<typename T > | |
T | findChild (const QString &childName=QString ()) const |
template<class T > | |
QList< T > | findChildren (const QRegularExpression ®Exp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
template<class T > | |
QList< T > | findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const |
bool | inherits (const QString &className) const |
void | installEventFilter (QObject *filterObj) |
bool | isWidgetType () const |
bool | isWindowType () const |
void | killTimer (int id) |
const QMetaObject * | metaObject () const |
void | moveToThread (QThread *targetThread) |
QString | objectName () const |
QObject * | parent () const |
template<class T = QVariant> | |
T | property (const QString &name) const |
void | removeEventFilter (QObject *obj) |
void | setObjectName (const QString &name) |
void | setParent (QObject *parent) |
bool | setProperty (const QString &name, const QVariant &value) |
bool | signalsBlocked () const |
int | startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer) |
QThread * | thread () const |
Static Public Methods | |
static void | addLibraryPath (const QString &path) |
static QString | applicationDirPath () |
static QString | applicationFilePath () |
static QString | applicationName () |
static qint64 | applicationPid () |
static QString | applicationVersion () |
static QStringList | arguments () |
static bool | closingDown () |
static int | exec () |
static void | exit (int returnCode=0) |
static void | flush () |
static void | installTranslator (QTranslator *translationFile) |
static QCoreApplication * | instance () |
static bool | isQuitLockEnabled () |
static bool | isSetuidAllowed () |
static QStringList | libraryPaths () |
static QString | organizationDomain () |
static QString | organizationName () |
static void | postEvent (QObject *receiver, QEvent *event, int priority=Qt::NormalEventPriority) |
static void | processEvents (QEventLoop::ProcessEventsFlags flags, int maxtime) |
static void | processEvents (QEventLoop::ProcessEventsFlags flags=QEventLoop::AllEvents) |
static void | removeLibraryPath (const QString &path) |
static void | removePostedEvents (QObject *receiver, int eventType=0) |
static void | removeTranslator (QTranslator *translationFile) |
static bool | sendEvent (QObject *receiver, QEvent *event) |
static void | sendPostedEvents (QObject *receiver=nullptr, int event_type=0) |
static void | setApplicationName (const QString &application) |
static void | setApplicationVersion (const QString &version) |
static void | setAttribute (Qt::ApplicationAttribute attribute, bool on=true) |
static void | setLibraryPaths (const QStringList &paths) |
static void | setOrganizationDomain (const QString &orgDomain) |
static void | setOrganizationName (const QString &orgName) |
static void | setQuitLockEnabled (bool enabled) |
static void | setSetuidAllowed (bool allow) |
static bool | startingUp () |
static bool | testAttribute (Qt::ApplicationAttribute attribute) |
static QString | translate (const char *context, const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >()) |
static QString | translate (const QString &context, const QString &text, const QString &comment=QString (), std::optional< int > numArg=std::optional< int >()) |
Static Public Methods inherited from QObject | |
static bool | connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ()) |
static bool | connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > | |
static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > | |
static bool | connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod) |
static bool | disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod) |
static bool | disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod) |
static bool | disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn > | |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...)) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver > | |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr) |
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T > | |
static bool | disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod) |
static QMetaObject & | staticMetaObject () |
static QString | tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >()) |
Protected Methods | |
bool | event (QEvent *) override |
Protected Methods inherited from QObject | |
virtual void | childEvent (QChildEvent *event) |
virtual void | connectNotify (const QMetaMethod &signalMethod) const |
virtual void | customEvent (QEvent *event) |
virtual void | disconnectNotify (const QMetaMethod &signalMethod) const |
bool | isSignalConnected (const QMetaMethod &signalMethod) const |
int | receivers (const QString &signal) const |
QObject * | sender () const |
int | senderSignalIndex () const |
virtual void | timerEvent (QTimerEvent *event) |
Properties | |
applicationName | |
applicationVersion | |
organizationDomain | |
organizationName | |
quitLockEnabled | |
Properties inherited from QObject | |
objectName | |
Friends | |
class | QApplication |
class | QWidget |
Related Functions | |
These are not member functions | |
void | qAddPostRoutine (QtCleanUpFunction ptr) |
void | qRemovePostRoutine (QtCleanUpFunction ptr) |
Related Functions inherited from QObject | |
T | qobject_cast (QObject *object) |
QObjectList | |
The QCoreApplication class provides an event loop for console applications. This class is used by non-GUI applications to provide their event loop. There should be exactly one QCoreApplication object for a given program. For GUI applications refer to QApplication.
QCoreApplication contains the main event loop, where all events from the operating system and other sources are processed and dispatched. For example, timer and network events are handled by this class. It also handles the application's initialization and finalization, as well as system wide and application wide settings.
The event loop is started with a call to exec(). Long running operations can call processEvents() to keep the application responsive.
In general it is recommended to create a QCoreApplication or a QApplication object in your main()
function as early as possible. Calling exec() will not return until the event loop exits, when quit() is called.
Several static methods are also provided. The QCoreApplication object is available by calling QCoreApplication::instance(). Events can be sent or posted using sendEvent(), postEvent(), and sendPostedEvents(). Pending events can be removed with removePostedEvents() or flushed with flush(). This class provides an aboutToQuit() signal and a quit() slot.
An application has an applicationDirPath() and an applicationFilePath(). Library paths (see QLibrary) can be retrieved with libraryPaths() and manipulated by setLibraryPaths(), addLibraryPath(), and removeLibraryPath().
Translation files can be added or removed using installTranslator() and removeTranslator(). Application strings can be translated using translate() or QObject::tr().
The command line arguments which are passed to the QCoreApplication constructor should be accessed using the arguments() method. Some of the arguments which were supplied by the user may be processed and removed by QCoreApplication.
The argv argument are processed by QCoreApplication and can be retrieved using the arguments() method.
On Unix systems CopperSpice is configured to use the system locale settings by default. This can cause a conflict when using POSIX functions. For example, when converting between data types such as floats and strings, the format may differ between locales.
To solve this, call the POSIX function setlocale(LC_NUMERIC, "C")
directly after initializing QApplication or QCoreApplication to set the locale for number formatting.
QCoreApplication::QCoreApplication | ( | int & | argc, |
char ** | argv | ||
) |
Constructs a CopperSpice application without a graphical user interface. This type of application is typically used for terminal or server process. The argc and argv arguments are processed by QCoreApplication and can be retrieved using the arguments() method.
QCoreApplication::~QCoreApplication | ( | ) |
Destroys the QCoreApplication object.
|
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 users shuts down the entire desktop session.
The signal is particularly useful if your application has to do some last-second cleanup. No user interaction is possible in this state.
|
static |
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 a single entry, the installation directory for plugins. The default installation directory for plugins is INSTALL/plugins
, where INSTALL
is the directory where CopperSpice was installed.
|
static |
Returns the directory which contains the application executable. The return value will be absolute or relative to the working directory. If present, any path separators will be a forward slash.
On Linux the path can be obtained from the /proc
file system. If that fails argv[0]
contains the absolute file name of the executable.
On Mac OS X the path will point to the directory actually containing the executable which may be inside of an application bundle.
|
static |
Returns the file path of the application executable.
On Linux this method 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 method assumes the current directory has not been changed by the application.
|
static |
Returns the value of the property applicationName.
|
static |
Returns the current process ID for the application.
|
static |
Returns the value of the property applicationVersion.
|
static |
Returns the list of command line arguments.
Usually arguments().at(0) is the program name and arguments().at(1) is the first argument and so on. Calling this method can be slow so you may want to store the result in a variable.
On Unix the return value is created from the argc and argv parameters passed to the QCoreApplication constructor. This method calls QString8::fromUtf8() to convert the argv data before saving it to the QStringList.
On Windows the return value is not built from the contents of argv and argc. Instead, the values are based on the return values of the GetCommandLine() function. As a result arguments().at(0) might not be the program name on Windows, depending on how the application was started.
|
static |
Returns true if the application objects are being destroyed, otherwise returns false.
|
overrideprotectedvirtual |
Receives events to an object and should return true if the given event was recognized and processed. This method can be overridden to customize the event handling for an object.
Reimplemented from QObject::event()
Reimplemented in QApplication::event()
|
static |
Enters the main event loop and waits until exit() is called. Returns the value that was set 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 (i.e. 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 aboutToQuit() signal, instead of putting it in your application's main()
function because on some platforms the QCoreApplication::exec() call may not return. For example, on Windows when the user logs off, the system terminates the process after CopperSpice 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 QCoreApplication::exec() call.
|
static |
Forces the application to exit with a return code. After calling this method the application leaves the main event loop and returns from the call to exec(). If the event loop is not running this method does nothing.
The exec() method will return the value returnCode. A returnCode of 0 means success and any non-zero value indicates an error.
Unlike the C library function of the same name this method does return to the caller and as a side effect stops the event loop.
|
static |
Flushes the platform specific event queues.
If you are doing graphical changes inside a loop that does not return to the event loop on asynchronous window systems like X11 or double buffered window systems like Mac OS X, and you want to visualize these changes immediately (e.g. Splash Screens), call this method.
void QCoreApplication::installNativeEventFilter | ( | QAbstractNativeEventFilter * | filterObj | ) |
Installs an event filter filterObj for all native events received by the application in the main thread. Each time a native event occurs a call is made to the nativeEventFilter() method located in the filterObj. This call only occurs in the main thread.
Returning false allows normal processing to continue. The native event is then converted into a QEvent and passed to the standard event system. If multiple event filters are installed the filter which was installed last is activated first. The nativeEventFilter() method receives native messages like "X11 MSG" or "XCB" event structs.
Native event filters will be disabled in the application when the Qt::AA_MacPluginApplication attribute is set.
When implementing nativeEventFilter(), returning true causes the event to be filtered and ignored. For portability use QEvent and QObject::installEventFilter() instead of the nativeEventFilter() method.
|
static |
Adds the translationFile to the list of existing files which will be used to translate a given piece of text. Multiple translation files can be installed and they are accessed in the reverse order in which they were installed. The search for a given piece of text stops when a translation containing a match is found.
Installing, removing, or changing an installed QTranslator generates a LanguageChange event in QCoreApplication. This event is propagated to all toplevel windows. If an override for the changeEvent() method exists, the window will be notified to update any text which is affected.
|
inlinestatic |
Returns a pointer to the application's QCoreApplication (or QApplication) instance. If no instance has been allocated a nullptr is returned.
|
static |
Returns the value of the property quitLockEnabled.
|
static |
Returns true if the application is allowed to run setuid on UNIX platforms.
|
static |
Returns a list of paths the application will search when dynamically loading libraries. It is not recommended to call this method before creating a QCoreApplication. The directory of the application executable (not the working directory) is part of the list if it is known.
CopperSpice provides default library paths, however they can also be set using a cs.conf file. Paths specified in this file will override default values. Refer to Plugin Path Configuration for more information.
This list will include the installation directory for plugins if it exists (the default installation directory for plugins is INSTALL/plugins
, where INSTALL
is the directory where CopperSpice was installed). The directory of the application executable (NOT the working directory) is always added, as well as the colon separated entries of the CS_PLUGIN_PATH environment variable.
The following example shows how to iterate over the list of library paths.
Sends event to receiver as if you had called "receiver->event(event)". Returns the value that is returned from the receiver's event handler. This method 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:
Reimplemented in QApplication::notify()
|
static |
Returns the value of the property organizationDomain.
|
static |
Returns the value of the property organizationName.
|
static |
Adds the given event with the object receiver as the receiver of the event, to an event queue and returns immediately. When control returns to the main event loop, all events that are stored in the queue will be sent using the notify() method.
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.
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. Refer to Qt::EventPriority for more details. Events with equal priority will be processed in the order posted.
This method is thread-safe.
|
static |
Processes pending events for the calling thread for maxtime milliseconds or until there are no more events to process, whichever is shorter. You can call this function occasionally when your program is busy doing a long operation such as copying a file.
This method is thread-safe.
|
static |
Processes all pending events for the calling thread according to the specified flags until there are no more events to process. You can call this function occasionally when your program is busy performing a long operation such as copying a file.
In event you are running a local loop which calls this function continuously, without an event loop, the DeferredDelete events will not be processed. This can affect the behavior of widgets, e.g. QToolTip, that rely on DeferredDelete events to function properly. An alternative would be to call sendPostedEvents() from within that local loop.
This method is thread-safe.
|
slot |
Instructs the application to exit with return code 0 (success). Equivalent to calling QCoreApplication::exit(0).
It is common to connect the QApplication::lastWindowClosed() signal to quit(). You may also want to connect other signals like QAbstractButton::clicked() or similar signals in QAction, QMenu, or QMenuBar to QApplication::lastWindowClosed().
|
static |
Removes path from the library path list. If path is empty or not in the path list, the list is not changed.
void QCoreApplication::removeNativeEventFilter | ( | QAbstractNativeEventFilter * | filterObj | ) |
Removes an event filterObj 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.
|
static |
Removes all events of the given eventType that were posted using postEvent() for receiver. If receiver is null, the events of eventType are removed for all objects. If eventType is 0, all the events are removed 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.
This method is thread-safe.
|
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.)
Sends the given event directly to 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 as shown in this sample code.
|
static |
Immediately dispatches all events which have been previously queued with QCoreApplication::postEvent() and which are intended for receiver and have the event type event_type. If receiver is a nullptr the events matching the event_type will be sent to every receiver. If event_type is 0 every events will be sent to the given receiver.
Events from the window system are not dispatched by this method, they are sent using processEvents().
|
static |
Sets the value of the property to application.
|
static |
Sets the value of the property to version.
|
static |
Sets the given attribute if on is true, otherwise clears the attribute.
One of the attributes that can be set with this method is Qt::Immediateness. CopperSpice will create a toplevel window immediately where as normally, resources for widgets are allocated on demand to improve efficiency and minimize resource usage. If it is important to minimize resource consumption, do not set this attribute.
|
static |
Sets the list of directories to search when loading libraries to paths. All existing paths will be deleted and the path list will consist of the paths given in paths.
|
static |
Sets the value of the property to orgDomain.
|
static |
Sets the value of the property to orgName.
|
static |
Sets the value of the property to enabled.
|
static |
Allows the application to run setuid on UNIX platforms if allow is true.
If allow is false (which is the default) and CopperSpice 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.
CopperSpice 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 CopperSpice from aborting the application when this is detected and must be set before a QCoreApplication instance is created.
It is strongly recommended not to enable this option since it introduces security risks.
|
static |
Returns true if an application object has not been created yet, otherwise returns false.
|
static |
Returns true if attribute is set, otherwise returns false.
|
static |
Returns the translation text by querying the installed translation files. If none of the translation files contain a translation for text this method returns the text untranslated.
The translation files are searched starting with the most recently installed and then progressing to older files. The parameter context is typically a class name. The optional parameter comment is an identifying string which is used when the same text can be translated differently depending on the context. By default the value is nullptr.
If a value for numArg was passed it will be substituted for the %n
in the translated string. This value is also used to choose the appropriate plural form in the translation. An example of the translated text (shown in English for readability) would be "1 file found" versus "5 files found".
Refer to Handle Plurals for additional information.
|
static |
The documentation for this overload is identical to the version of translate() which declares the first three parameters as "const char *".
|
signal |
This signal is emitted whenever a Unix signal is received by the application. The given signalId refers to which signal was received. Refer to the POSIX standard for a full list of signal ids.
|
related |
Adds a global routine which will be called from the QApplication destructor. This function is normally used to add cleanup routines for program wide functionality.
The function specified by ptr should take no arguments and return nothing.
For an application or module wide cleanup this function is often not suitable. For example, if the program is split into dynamically loaded modules, the relevant module may be unloaded long before the QApplication destructor is called.
For modules and libraries, using a reference-counted initialization manager or the 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.
|
related |
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.
|
This property holds the name of the current application. The property value is used by the QSettings default constructor.
Properties | Class Methods |
---|---|
read | applicationName |
write | setApplicationName |
|
This property holds the version of the current application.
Properties | Class Methods |
---|---|
read | applicationVersion |
write | setApplicationVersion |
|
This property holds the Internet domain of the organization that wrote the current application. The property value is used by the QSettings default constructor.
On Mac, QSettings uses organizationDomain() as the organization if it is not an empty string, otherwise it uses organizationName().
Properties | Class Methods |
---|---|
read | organizationDomain |
write | setOrganizationDomain |
|
This property holds the name of the organization who developed the current application. The property value is used by the QSettings default constructor.
On Mac OS X QSettings uses organizationDomain() as the organization if it is not an empty string, otherwise it uses organizationName().
Properties | Class Methods |
---|---|
read | organizationName |
write | setOrganizationName |
|
This property holds whether the use of QEventLoopLocker can cause the application to shut down. The default is true.
Properties | Class Methods |
---|---|
read | isQuitLockEnabled |
write | setQuitLockEnabled |