6.0.0-rc4Source

QApplicationStatic Class

template <typename Holder> class QApplicationStatic

The QApplicationStatic class holds a global object that lives as long as the application object. More…

Header#include <QApplicationStatic>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
Since6.3

List of all members, including inherited members

Public Types

typedefType

Public Functions

boolexists() const
operator Type*()
Type *operator()()
Type *operator->()
Type &operator*()
voidreset()

Detailed Description

Programs use it through the Q_APPLICATION_STATIC macro and do not name the class. The macro defines a variable that behaves like a pointer to an object of the given type; the arguments after the name, if any, are passed to its constructor. The object is created the first time it is used and destroyed with the QCoreApplication, which makes it the right kind of global for a QObject or anything else that needs the application:

Q_APPLICATION_STATIC(QTimer, heartbeat)

void startHeartbeat() { heartbeat->start(1000); }

If the program creates another application object later, the next use creates a new object.

See also QGlobalStatic and QCoreApplication.

Differences from Qt

An object that is first used when there is no application object is created all the same, and destroyed with the next application object; Qt requires the application object to exist. The macro defines a static variable in the file that uses it, so declare each object once, in a .cpp file.

Member Type Documentation

typedef QApplicationStatic::Type

The type of the object that the variable stands for.

Member Function Documentation

bool QApplicationStatic::exists() const

Returns true if the object exists now: it has been used and not been destroyed since.

QApplicationStatic::operator Type*()

Returns a pointer to the object, creating it if needed.

Type * QApplicationStatic::operator()()

Returns a pointer to the object, creating it if needed.

Type & QApplicationStatic::operator*()

Returns a reference to the object, creating it if needed.

Type * QApplicationStatic::operator->()

Returns a pointer to the object, creating it if needed.

void QApplicationStatic::reset()

Destroys the object now, if it exists. The next use creates a new one.