QMetaObject Struct | Qt Core (original) (raw)

Member Function Documentation

[static, since 6.5] template <typename... Args> bool QMetaObject::invokeMethod(QObject *obj, const char *member, Args &&... args)

[static, since 6.5] template <typename ReturnArg, typename... Args> bool QMetaObject::invokeMethod(QObject *obj, const char *member, QTemplatedMetaMethodReturnArgument<ReturnArg> ret, Args &&... args)

[static, since 6.5] template <typename... Args> bool QMetaObject::invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, Args &&... args)

[static, since 6.5] template <typename ReturnArg, typename... Args> bool QMetaObject::invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QTemplatedMetaMethodReturnArgument<ReturnArg> ret, Args &&... args)

Invokes the member (a signal or a slot name) on the object obj. Returns true if the member could be invoked. Returns false if there is no such member or the parameters did not match.

For the overloads with a QTemplatedMetaMethodReturnArgument parameter, the return value of the member function call is placed in ret. For the overloads without such a member, the return value of the called function (if any) will be discarded. QTemplatedMetaMethodReturnArgument is an internal type you should not use directly. Instead, use the qReturnArg() function.

The overloads with a Qt::ConnectionType type parameter allow explicitly selecting whether the invocation will be synchronous or not:

You only need to pass the name of the signal or slot to this function, not the entire signature. For example, to asynchronously invoke the quit() slot on a QThread, use the following code:

QMetaObject::invokeMethod(thread, "quit", Qt::QueuedConnection);

With asynchronous method invocations, the parameters must be copyable types, because Qt needs to copy the arguments to store them in an event behind the scenes. Since Qt 6.5, this function automatically registers the types being used; however, as a side-effect, it is not possible to make calls using types that are only forward-declared. Additionally, it is not possible to make asynchronous calls that use references to non-const-qualified types as parameters either.

To synchronously invoke the compute(QString, int, double) slot on some arbitrary object obj retrieve its return value:

QString retVal; QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection, qReturnArg(retVal), QString("sqrt"), 42, 9.7);

If the "compute" slot does not take exactly one QString, one int, and one double in the specified order, the call will fail. Note how it was necessary to be explicit about the type of the QString, as the character literal is not exactly the right type to match. If the method instead took a QStringView, a qsizetype, and a float, the call would need to be written as:

The same call can be executed using the Q_ARG() and Q_RETURN_ARG() macros, as in:

QString retVal; QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection, Q_RETURN_ARG(QString, retVal), Q_ARG(QString, "sqrt"), Q_ARG(int, 42), Q_ARG(double, 9.7));

The macros are kept for compatibility with Qt 6.4 and earlier versions, and can be freely mixed with parameters that do not use the macro. They may be necessary in rare situations when calling a method that used a typedef to forward-declared type as a parameter or the return type.

Note: This function is thread-safe.

This function was introduced in Qt 6.5.

See also Q_ARG(), Q_RETURN_ARG(), and QMetaMethod::invoke().

[static] template <typename Functor, typename FunctorReturnType> bool QMetaObject::invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)

[static] template <typename Functor, typename FunctorReturnType> bool QMetaObject::invokeMethod(QObject *context, Functor &&function, Qt::ConnectionType type = Qt::AutoConnection, FunctorReturnType *ret = nullptr)

Invokes the function in the event loop of context. function can be a functor or a pointer to a member function. Returns true if the function could be invoked. Returns false if there is no such function or the parameters did not match. The return value of the function call is placed in ret.

If type is set, then the function is invoked using that connection type. Otherwise, Qt::AutoConnection will be used.

Note: This function is thread-safe.

[static, since 6.7] template <typename Functor, typename... Args> bool QMetaObject::invokeMethod(QObject *context, Functor &&function, Args &&... arguments)

[static, since 6.7] template <typename Functor, typename FunctorReturnType, typename... Args> bool QMetaObject::invokeMethod(QObject *context, Functor &&function, QTemplatedMetaMethodReturnArgument<FunctorReturnType> ret, Args &&... arguments)

[static, since 6.7] template <typename Functor, typename... Args> bool QMetaObject::invokeMethod(QObject *context, Functor &&function, Qt::ConnectionType type, Args &&... arguments)

[static, since 6.7] template <typename Functor, typename FunctorReturnType, typename... Args> bool QMetaObject::invokeMethod(QObject *context, Functor &&function, Qt::ConnectionType type, QTemplatedMetaMethodReturnArgument<FunctorReturnType> ret, Args &&... arguments)

Invokes the function with arguments in the event loop of context. function can be a functor or a pointer to a member function. Returns true if the function could be invoked. The return value of the function call is placed in ret. The object used for the ret argument should be obtained by passing your object to qReturnArg(). For example:

MyClass *obj = ...; int result = 0; QMetaObject::invokeMethod(obj, &MyClass::myMethod, qReturnArg(result), parameter);

If type is set, then the function is invoked using that connection type. Otherwise, Qt::AutoConnection will be used.

Note: This function is thread-safe.

This function was introduced in Qt 6.7.

[static] bool QMetaObject::checkConnectArgs(const char *signal, const char *method)

Returns true if the signal and method arguments are compatible; otherwise returns false.

Both signal and method are expected to be normalized.

See also normalizedSignature().

[static] bool QMetaObject::checkConnectArgs(const QMetaMethod &signal, const QMetaMethod &method)

This is an overloaded function.

Returns true if the signal and method arguments are compatible; otherwise returns false.

QMetaClassInfo QMetaObject::classInfo(int index) const

Returns the meta-data for the item of class information with the given index.

Example:

class MyClass : public QObject { Q_OBJECT Q_CLASSINFO("author", "Sabrina Schweinsteiger") Q_CLASSINFO("url", "http://doc.moosesoft.co.uk/1.0/")

public: ... };

See also classInfoCount(), classInfoOffset(), and indexOfClassInfo().

int QMetaObject::classInfoCount() const

Returns the number of items of class information in this class.

See also classInfo(), classInfoOffset(), and indexOfClassInfo().

int QMetaObject::classInfoOffset() const

Returns the class information offset for this class; i.e. the index position of this class's first class information item.

If the class has no superclasses with class information, the offset is 0; otherwise the offset is the sum of all the class information items in the class's superclasses.

See also classInfo(), classInfoCount(), and indexOfClassInfo().

const char *QMetaObject::className() const

Returns the class name.

See also superClass().

[static] void QMetaObject::connectSlotsByName(QObject *object)

Searches recursively for all child objects of the given object, and connects matching signals from them to slots of object that follow the following form:

void on__();

Let's assume our object has a child object of type QPushButton with the object name button1. The slot to catch the button's clicked() signal would be:

void on_button1_clicked();

If object itself has a properly set object name, its own signals are also connected to its respective slots.

See also QObject::setObjectName().

QMetaMethod QMetaObject::constructor(int index) const

Returns the meta-data for the constructor with the given index.

See also constructorCount() and newInstance().

int QMetaObject::constructorCount() const

Returns the number of constructors in this class.

See also constructor() and indexOfConstructor().

QMetaEnum QMetaObject::enumerator(int index) const

Returns the meta-data for the enumerator with the given index.

See also enumeratorCount(), enumeratorOffset(), and indexOfEnumerator().

int QMetaObject::enumeratorCount() const

Returns the number of enumerators in this class.

See also enumerator(), enumeratorOffset(), and indexOfEnumerator().

int QMetaObject::enumeratorOffset() const

Returns the enumerator offset for this class; i.e. the index position of this class's first enumerator.

If the class has no superclasses with enumerators, the offset is 0; otherwise the offset is the sum of all the enumerators in the class's superclasses.

See also enumerator(), enumeratorCount(), and indexOfEnumerator().

int QMetaObject::indexOfClassInfo(const char *name) const

Finds class information item name and returns its index; otherwise returns -1.

See also classInfo(), classInfoCount(), and classInfoOffset().

int QMetaObject::indexOfConstructor(const char *constructor) const

Finds constructor and returns its index; otherwise returns -1.

Note that the constructor has to be in normalized form, as returned by normalizedSignature().

See also constructor(), constructorCount(), and normalizedSignature().

int QMetaObject::indexOfEnumerator(const char *name) const

Finds enumerator name and returns its index; otherwise returns -1.

See also enumerator(), enumeratorCount(), and enumeratorOffset().

int QMetaObject::indexOfMethod(const char *method) const

Finds method and returns its index; otherwise returns -1.

Note that the method has to be in normalized form, as returned by normalizedSignature().

See also method(), methodCount(), methodOffset(), and normalizedSignature().

int QMetaObject::indexOfProperty(const char *name) const

Finds property name and returns its index; otherwise returns -1.

See also property(), propertyCount(), and propertyOffset().

int QMetaObject::indexOfSignal(const char *signal) const

Finds signal and returns its index; otherwise returns -1.

This is the same as indexOfMethod(), except that it will return -1 if the method exists but isn't a signal.

Note that the signal has to be in normalized form, as returned by normalizedSignature().

See also indexOfMethod(), normalizedSignature(), method(), methodCount(), and methodOffset().

int QMetaObject::indexOfSlot(const char *slot) const

Finds slot and returns its index; otherwise returns -1.

This is the same as indexOfMethod(), except that it will return -1 if the method exists but isn't a slot.

See also indexOfMethod(), method(), methodCount(), and methodOffset().

[noexcept] bool QMetaObject::inherits(const QMetaObject *metaObject) const

Returns true if the class described by this QMetaObject inherits the type described by metaObject; otherwise returns false.

A type is considered to inherit itself.

[since 6.2] QMetaType QMetaObject::metaType() const

Returns the metatype corresponding to this metaobject. If the metaobject originates from a namespace, an invalid metatype is returned.

This function was introduced in Qt 6.2.

QMetaMethod QMetaObject::method(int index) const

Returns the meta-data for the method with the given index.

See also methodCount(), methodOffset(), and indexOfMethod().

int QMetaObject::methodCount() const

Returns the number of methods in this class, including the number of methods provided by each base class. These include signals and slots as well as normal member functions.

Use code like the following to obtain a QStringList containing the methods specific to a given class:

const QMetaObject* metaObject = obj->metaObject(); QStringList methods; for(int i = metaObject->methodOffset(); i < metaObject->methodCount(); ++i) methods << QString::fromLatin1(metaObject->method(i).methodSignature());

See also method(), methodOffset(), and indexOfMethod().

int QMetaObject::methodOffset() const

Returns the method offset for this class; i.e. the index position of this class's first member function.

The offset is the sum of all the methods in the class's superclasses (which is always positive since QObject has the deleteLater() slot and a destroyed() signal).

See also method(), methodCount(), and indexOfMethod().

[since 6.5] template <typename... Args> QObject *QMetaObject::newInstance(Args &&... arguments) const

Constructs a new instance of this class and returns the new object, or nullptr if no suitable constructor is available. The types of the arguments arguments will be used to find a matching constructor, and then forwarded to it the same way signal-slot connections do.

Note that only constructors that are declared with the Q_INVOKABLE modifier are made available through the meta-object system.

This function was introduced in Qt 6.5.

See also constructor().

[static] QByteArray QMetaObject::normalizedSignature(const char *method)

Normalizes the signature of the given method.

Qt uses normalized signatures to decide whether two given signals and slots are compatible. Normalization reduces whitespace to a minimum, moves 'const' to the front where appropriate, removes 'const' from value types and replaces const references with values.

See also checkConnectArgs() and normalizedType().

[static] QByteArray QMetaObject::normalizedType(const char *type)

Normalizes a type.

See QMetaObject::normalizedSignature() for a description on how Qt normalizes.

Example:

See also normalizedSignature().

QMetaProperty QMetaObject::property(int index) const

Returns the meta-data for the property with the given index. If no such property exists, a null QMetaProperty is returned.

See also propertyCount(), propertyOffset(), and indexOfProperty().

int QMetaObject::propertyCount() const

Returns the number of properties in this class, including the number of properties provided by each base class.

Use code like the following to obtain a QStringList containing the properties specific to a given class:

const QMetaObject* metaObject = obj->metaObject(); QStringList properties; for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); ++i) properties << QString::fromLatin1(metaObject->property(i).name());

See also property(), propertyOffset(), and indexOfProperty().

int QMetaObject::propertyOffset() const

Returns the property offset for this class; i.e. the index position of this class's first property.

The offset is the sum of all the properties in the class's superclasses (which is always positive since QObject has the objectName property).

See also property(), propertyCount(), and indexOfProperty().

const QMetaObject *QMetaObject::superClass() const

Returns the meta-object of the superclass, or nullptr if there is no such object.

See also className().

QMetaProperty QMetaObject::userProperty() const

Returns the property that has the USER flag set to true.

See also QMetaProperty::isUser().