6.0.0-rc4Source

QGlobalStatic Class

template <typename Holder> class QGlobalStatic

The QGlobalStatic class is used to implement a global static object. More…

Header#include <QGlobalStatic>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

typedefType

Public Functions

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

Detailed Description

Applications use it through the Q_GLOBAL_STATIC and Q_GLOBAL_STATIC_WITH_ARGS macros, not by naming the class. The macros define a variable that behaves like a pointer to an object of the given type. The object is created the first time it is used, in a thread-safe way, and destroyed when the program exits:

Q_GLOBAL_STATIC(QStringList, sharedNames)

void remember(const QString &name) { sharedNames->append(name); }

Unlike a plain static variable, the object is never constructed if it is not used, and code that runs during the program's shutdown can find out that it is gone with isDestroyed(): the pointer is then nullptr.

Differences from Qt

None in behavior. The macros define a static variable in the file that uses them, so declare each global once, in a .cpp file.

Member Type Documentation

typedef QGlobalStatic::Type

The type of the object that the variable stands for.

Member Function Documentation

bool QGlobalStatic::exists() const

Returns true if the object has been created and not yet destroyed.

See also isDestroyed().

bool QGlobalStatic::isDestroyed() const

Returns true if the object has been destroyed, which happens when the program exits.

See also exists().

QGlobalStatic::operator Type*()

Returns a pointer to the object, creating it if necessary, or nullptr if it has been destroyed.

Type * QGlobalStatic::operator()()

Returns a pointer to the object, creating it if necessary, or nullptr if it has been destroyed.

Type & QGlobalStatic::operator*()

Returns a reference to the object, creating it if necessary. Calling this after the object has been destroyed is undefined.

Type * QGlobalStatic::operator->()

Returns a pointer to the object, creating it if necessary, or nullptr if it has been destroyed.