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 method (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.7 起) bool invokeMethod (QObject * context , Functor && function , Args &&... arguments )
bool invokeMethod (QObject * context , Functor && function , FunctorReturnType * ret )
(从 6.5 起) bool invokeMethod (QObject * obj , const char * member , Args &&... args )
(从 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 )
bool invokeMethod (QObject * context , Functor && function , Qt::ConnectionType type = Qt::AutoConnection, FunctorReturnType * ret = nullptr)
(从 6.5 起) bool invokeMethod (QObject * obj , const char * member , QTemplatedMetaMethodReturnArgument<ReturnArg> ret , Args &&... args )
(从 6.5 起) bool invokeMethod (QObject * obj , const char * member , Qt::ConnectionType type , Args &&... args )
(从 6.7 起) bool invokeMethod (QObject * context , Functor && function , Qt::ConnectionType type , QTemplatedMetaMethodReturnArgument<FunctorReturnType> ret , Args &&... arguments )
(从 6.5 起) bool invokeMethod (QObject * obj , const char * member , Qt::ConnectionType type , QTemplatedMetaMethodReturnArgument<ReturnArg> ret , Args &&... args )
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 若没有这样的成员或参数不匹配。

对于具有 QTemplatedMetaMethodReturnArgument 参数的重载,返回值对于 member 函数调用被放置在 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.

重载采用 Qt::ConnectionType type 参数允许明确选择是否将同步援引:

  • type is Qt::DirectConnection , 成员将在当前线程中被立即援引。
  • 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 在其中被创建或被移动到。
  • 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) 槽在某些任意对象 obj 检索其返回值:

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

若 compute 槽不接受 1 个准确 QString , one int ,和 1 个 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.

注意: 此函数是 线程安全 .

该函数在 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 若不存在这种函数 (或参数不匹配)。函数调用的返回值是放在 ret .

type 有设置,那么使用该连接类型援引函数。否则, Qt::AutoConnection 会被使用。

注意: 此函数是 线程安全 .

[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 若函数可以被援引。函数调用的返回值是放在 ret 。使用的对象对于 ret 自变量应该通过把对象传递给 qReturnArg() 获得。例如:

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

type 有设置,那么使用该连接类型援引函数。否则, Qt::AutoConnection 会被使用。

注意: 此函数是 线程安全 .

该函数在 Qt 6.7 引入。

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

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

Both signal and method 期望被规范化。

另请参阅 normalizedSignature ().

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

这是重载函数。

返回 truesignal 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

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

若类没有带枚举的超类,偏移为 0;否则,偏移是类的超类的所有枚举的和。

另请参阅 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 ().

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

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

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

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

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

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

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

类型被认为继承本身。

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

返回此元对象对应的元类型。若元对象发源自命名空间,返回无效元类型。

该函数在 Qt 6.2 引入。

QMetaMethod QMetaObject:: method ( int index ) const

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

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

int QMetaObject:: methodCount () const

返回此类的方法数,包括每个基类提供的方法数。这些包括信号-槽,及正常成员函数。

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

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

另请参阅 method (), methodOffset (),和 indexOfMethod ().

int QMetaObject:: methodOffset () const

返回此类的方法偏移;即:此类第一成员函数的索引位置。

偏移是类的超类所有方法的和 (始终正值,因为 QObject 拥有 deleteLater () 槽和 destroyed () 信号)。

另请参阅 method (), methodCount (),和 indexOfMethod ().

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

构造此类的新实例并返回新对象,或 nullptr 若没有合适的构造函数可用。类型对于自变量 arguments 将被用于查找匹配的构造函数,然后转发给它,与信号-槽连接所做的一样。

注意,仅构造函数的声明采用 Q_INVOKABLE 修饰符是可用的透过元对象系统。

该函数在 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

返回此类的属性偏移;即:此类第一特性的索引位置。

偏移是类的超类的所有特性的和 (始终正值,因为 QObject 拥有 objectName 特性)。

另请参阅 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 对于该类型并返回 QMetaMethodArgument,可以将其传递给模板 QMetaObject::invokeMethod () 采用 Args &&... 自变量。

另请参阅 Q_RETURN_ARG ().

QMetaMethodReturnArgument Q_RETURN_ARG ( Type , Type & value )

此宏接受 Type 和非常量引用为 value 对于该类型并返回 QMetaMethodReturnArgument,可以将其传递给模板 QMetaObject::invokeMethod () 采用 Args &&... 自变量。

另请参阅 Q_ARG ().