QMetaObject 结构

QMetaObject 类包含有关 Qt 对象的元信息。 更多...

头: #include <QMetaObject>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

公共类型

class Connection

公共函数

QMetaClassInfo classInfo (int index ) const
int classInfoCount () const
int classInfoOffset () const
const char * className () const
QMetaMethod constructor (int index ) const
int constructorCount () const
QMetaEnum enumerator (int index ) const
int enumeratorCount () const
int enumeratorOffset () const
int indexOfClassInfo (const char * name ) const
int indexOfConstructor (const char * constructor ) const
int indexOfEnumerator (const char * name ) const
int indexOfMethod (const char * method ) const
int indexOfProperty (const char * name ) const
int indexOfSignal (const char * signal ) const
int indexOfSlot (const char * slot ) const
bool inherits (const QMetaObject * metaObject ) const
(从 6.2 起) QMetaType metaType () const
QMetaMethod 方法 (int index ) const
int methodCount () const
int methodOffset () const
(从 6.5 起) QObject * newInstance (Args &&... arguments ) const
QMetaProperty property (int index ) const
int propertyCount () const
int propertyOffset () const
const QMetaObject * superClass () const
QMetaProperty userProperty () const

静态公共成员

bool checkConnectArgs (const char * signal , const char * method )
bool checkConnectArgs (const QMetaMethod & signal , const QMetaMethod & method )
void connectSlotsByName (QObject * object )
(从 6.5 起) bool invokeMethod (QObject * obj , const char * member , Qt::ConnectionType type , QTemplatedMetaMethodReturnArgument<ReturnArg> ret , Args &&... args )
(从 6.5 起) bool invokeMethod (QObject * obj , const char * member , Qt::ConnectionType type , Args &&... args )
(从 6.5 起) bool invokeMethod (QObject * obj , const char * member , QTemplatedMetaMethodReturnArgument<ReturnArg> ret , Args &&... args )
(从 6.5 起) bool invokeMethod (QObject * obj , const char * member , Args &&... args )
bool invokeMethod (QObject * context , Functor && function , Qt::ConnectionType type = Qt::AutoConnection, FunctorReturnType * ret = nullptr)
bool invokeMethod (QObject * context , Functor && function , FunctorReturnType * ret )
(从 6.7 起) bool invokeMethod (QObject * context , Functor && function , Qt::ConnectionType type , QTemplatedMetaMethodReturnArgument<FunctorReturnType> ret , Args &&... arguments )
(从 6.7 起) bool invokeMethod (QObject * context , Functor && function , QTemplatedMetaMethodReturnArgument<FunctorReturnType> ret , Args &&... arguments )
(从 6.7 起) bool invokeMethod (QObject * context , Functor && function , Qt::ConnectionType type , Args &&... arguments )
(从 6.7 起) bool invokeMethod (QObject * context , Functor && function , Args &&... arguments )
QByteArray normalizedSignature (const char * method )
QByteArray normalizedType (const char * type )

QMetaMethodArgument Q_ARG ( Type , const Type & value )
QMetaMethodReturnArgument Q_RETURN_ARG ( Type , Type & value )

详细描述

Qt 元对象系统 在 Qt 中是负责信号/槽对象间的通信机制、运行时类型信息、及 Qt 特性系统。创建单 QMetaObject 实例为每个 QObject 在应用程序中使用的子类,且此实例存储所有元信息为 QObject 子类。此对象是可用的作为 QObject::metaObject ().

应用程序编程通常不要求此类,但它很有用若编写元应用程 (譬如:脚本引擎或 GUI 构建器)。

最可能找到的有用函数是这些:

索引函数 indexOfConstructor (), indexOfMethod (), indexOfEnumerator (),和 indexOfProperty () 将构造函数名称、成员函数、枚举器或特性映射到元对象中的索引。例如,Qt 使用 indexOfMethod () 在内部当把信号连接到槽时。

类也可以拥有列表化的 namevalue 对的额外类信息,存储在 QMetaClassInfo 对象。对数的返回通过 classInfoCount (),单个对的返回通过 classInfo (),和可以搜索对采用 indexOfClassInfo ().

注意: 使用元对象系统的操作一般是线程安全的,因为 QMetaObject 通常是在编译时生成的静态只读实例。不管怎样,若通过应用程序动态修改元对象 (例如,当使用 QQmlPropertyMap ),那么应用程序必须明确同步访问各自的元对象。

另请参阅 QMetaClassInfo , QMetaEnum , QMetaMethod , QMetaProperty , QMetaType ,和 元对象系统 .

成员函数文档编制

[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 )

援引 member (信号或槽名称) 在对象 obj 。返回 true 若成员可以被援引。返回 false 若没有这样的成员或参数不匹配。

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:

  • type is Qt::DirectConnection , the member will be invoked immediately in the current thread.
  • type is Qt::QueuedConnection QEvent will be sent and the member is invoked as soon as the application enters the event loop in the thread that the obj was created in or was moved to.
  • type is Qt::BlockingQueuedConnection , the method will be invoked in the same way as for Qt::QueuedConnection , except that the current thread will block until the event is delivered. Using this connection type to communicate between objects in the same thread will lead to deadlocks.
  • type is Qt::AutoConnection , the member is invoked synchronously if obj lives in the same thread as the caller; otherwise it will invoke the member asynchronously. This is the behavior of the overloads that do not have the type 参数。

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 ,使用以下代码:

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.

要同步援引 compute(QString, int, double) slot on some arbitrary object obj 检索其返回值:

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

若 compute 槽不接受恰好一个 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 qsizetype ,和 float , the call would need to be written as:

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

The same call can be executed using the Q_ARG () 和 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.

注意: 此函数是 thread-safe .

该函数在 Qt 6.5 引入。

另请参阅 Q_ARG (), Q_RETURN_ARG (),和 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)

援引 function 在事件循环的 context . function 可以是函子 (或指向成员函数的指针)。返回 true 若函数可以被援引。返回 false if there is no such function or the parameters did not match. The return value of the function call is placed in ret .

type is set, then the function is invoked using that connection type. Otherwise, Qt::AutoConnection 会被使用。

注意: 此函数是 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 )

援引 function with arguments 在事件循环的 context . function 可以是函子 (或指向成员函数的指针)。返回 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);
					

type is set, then the function is invoked using that connection type. Otherwise, Qt::AutoConnection 会被使用。

注意: 此函数是 thread-safe .

该函数在 Qt 6.7 引入。

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

返回 true signal and method 自变量兼容;否则返回 false .

Both signal and method 期望被规范化。

另请参阅 normalizedSignature ().

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

这是重载函数。

返回 true signal and method 自变量兼容;否则返回 false .

QMetaClassInfo QMetaObject:: classInfo ( int index ) const

返回用于类信息项的元数据,采用给定 index .

范例:

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

另请参阅 classInfoCount (), classInfoOffset (),和 indexOfClassInfo ().

int QMetaObject:: classInfoCount () const

返回此类类信息的项数。

另请参阅 classInfo (), classInfoOffset (),和 indexOfClassInfo ().

int QMetaObject:: classInfoOffset () const

返回用于此类的类信息偏移量;即:此类的第一类信息项的索引位置。

若类是没有类信息的超级类,偏移为 0;否则,偏移是类的超级类的所有类信息项的和。

另请参阅 classInfo (), classInfoCount (),和 indexOfClassInfo ().

const char *QMetaObject:: className () const

返回类名。

另请参阅 superClass ().

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

递归搜索所有子级对象为给定 object ,并将来自它们的匹配信号连接到槽对于 object 遵循以下形式:

void on_<object name>_<signal name>(<signal parameters>);
					

假定对象拥有的子级对象类型为 QPushButton 采用 对象名称 button1 。槽相对捕获按钮 clicked() 信号将是:

void on_button1_clicked();
					

object 本身有正确设置对象名称,还会将其信号分别连接到它的槽。

另请参阅 QObject::setObjectName ().

QMetaMethod QMetaObject:: constructor ( int index ) const

返回构造函数元数据按给定 index .

另请参阅 constructorCount () 和 newInstance ().

int QMetaObject:: constructorCount () const

返回此类的构造函数数。

另请参阅 constructor () 和 indexOfConstructor ().

QMetaEnum QMetaObject:: enumerator ( int index ) const

返回枚举器元数据按给定 index .

另请参阅 enumeratorCount (), enumeratorOffset (),和 indexOfEnumerator ().

int QMetaObject:: enumeratorCount () const

返回此类中的枚举器数。

另请参阅 enumerator (), enumeratorOffset (),和 indexOfEnumerator ().

int QMetaObject:: enumeratorOffset () const

返回用于此类的枚举偏移;即:此类的第一枚举器的索引位置。

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.

另请参阅 enumerator (), enumeratorCount (),和 indexOfEnumerator ().

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

查找类信息项 name 并返回其索引;否则返回 -1。

另请参阅 classInfo (), classInfoCount (),和 classInfoOffset ().

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

查找 constructor 并返回其索引;否则返回 -1。

注意, constructor 必须是规范化形式,作为返回通过 normalizedSignature ().

另请参阅 constructor (), constructorCount (),和 normalizedSignature ().

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

查找枚举器 name 并返回其索引;否则返回 -1。

另请参阅 enumerator (), enumeratorCount (),和 enumeratorOffset ().

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

查找 method 并返回其索引;否则返回 -1。

注意, method 必须是规范化形式,作为返回通过 normalizedSignature ().

另请参阅 方法 (), methodCount (), methodOffset (),和 normalizedSignature ().

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

查找特性 name 并返回其索引;否则返回 -1。

另请参阅 property (), propertyCount (),和 propertyOffset ().

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

查找 signal 并返回其索引;否则返回 -1。

这如同 indexOfMethod (),除了它会返回 -1,若方法存在但不是信号。

注意, signal 必须是规范化形式,作为返回通过 normalizedSignature ().

另请参阅 indexOfMethod (), normalizedSignature (), 方法 (), methodCount (),和 methodOffset ().

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

查找 slot 并返回其索引;否则返回 -1。

这如同 indexOfMethod (),除了它会返回 -1,若方法存在但不是槽。

另请参阅 indexOfMethod (), 方法 (), methodCount (),和 methodOffset ().

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

返回 true 若类描述通过此 QMetaObject 继承类型描述通过 metaObject ;否则返回 false。

类型被认为继承本身。

[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.

该函数在 Qt 6.2 引入。

QMetaMethod QMetaObject:: 方法 ( int index ) const

返回用于方法的元数据按给定 index .

另请参阅 methodCount (), methodOffset (),和 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.

使用像以下代码获得的 QStringList 包含给定类特定方法:

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

另请参阅 方法 (), methodOffset (),和 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).

另请参阅 方法 (), methodCount (),和 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.

该函数在 Qt 6.5 引入。

另请参阅 constructor ().

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

规范化签名为给定 method .

Qt 使用规范化签名决定 2 给定信号/槽是否兼容。规范化将空白减少到最小,将 const 移到适当位置前,从值类型移除 const,并采用值替换 const 引用。

另请参阅 checkConnectArgs () 和 normalizedType ().

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

规范化 type .

QMetaObject::normalizedSignature () 了解 Qt 如何规范化的描述。

范例:

QByteArray normType = QMetaObject::normalizedType(" int    const  *");
// normType is now "const int*"
					

另请参阅 normalizedSignature ().

QMetaProperty QMetaObject:: property ( int index ) const

返回元数据若特性具有给定 index 。若没有这种特性存在,null QMetaProperty 被返回。

另请参阅 propertyCount (), propertyOffset (),和 indexOfProperty ().

int QMetaObject:: propertyCount () const

返回此类的特性数,包括每个基类提供的特性数。

使用像以下代码获得的 QStringList 包含特定于给定类的特性:

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

另请参阅 property (), propertyOffset (),和 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 拥有 name() 特性)。

另请参阅 property (), propertyCount (),和 indexOfProperty ().

const QMetaObject *QMetaObject:: superClass () const

返回超类的元对象,或 nullptr 若没有这种对象。

另请参阅 className ().

QMetaProperty QMetaObject:: userProperty () const

返回特性拥有 USER 标志被设为 true。

另请参阅 QMetaProperty::isUser ().

宏文档编制

QMetaMethodArgument Q_ARG ( Type , const Type & value )

此宏接受 Typevalue of that type and returns a QMetaMethodArgument, which can be passed to the template QMetaObject::invokeMethod () 采用 Args &&... 自变量。

另请参阅 Q_RETURN_ARG ().

QMetaMethodReturnArgument Q_RETURN_ARG ( Type , Type & value )

此宏接受 Type and a non-const reference to a value of that type and returns a QMetaMethodReturnArgument, which can be passed to the template QMetaObject::invokeMethod () 采用 Args &&... 自变量。

另请参阅 Q_ARG ().