QMetaObject Struct
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 |
(since 6.2) QMetaType | metaType() const |
| QMetaMethod | method(int index) const |
| int | methodCount() const |
| int | methodOffset() const |
(since 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) |
(since 6.10) QMetaObject::Connection | connect(const QObject *sender, const QMetaMethod &signal, const QObject *context, Functor functor, Qt::ConnectionType type = Qt::AutoConnection) |
(since 6.10) QMetaObject::Connection | connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection) |
| void | connectSlotsByName(QObject *object) |
(since 6.7) bool | invokeMethod(QObject *context, Functor &&function, Args &&... arguments) |
| bool | invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret) |
(since 6.5) bool | invokeMethod(QObject *obj, const char *member, Args &&... args) |
(since 6.7) bool | invokeMethod(QObject *context, Functor &&function, QTemplatedMetaMethodReturnArgument<FunctorReturnType> ret, Args &&... arguments) |
(since 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) |
(since 6.5) bool | invokeMethod(QObject *obj, const char *member, QTemplatedMetaMethodReturnArgument<ReturnArg> ret, Args &&... args) |
(since 6.5) bool | invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, Args &&... args) |
(since 6.7) bool | invokeMethod(QObject *context, Functor &&function, Qt::ConnectionType type, QTemplatedMetaMethodReturnArgument<FunctorReturnType> ret, Args &&... arguments) |
(since 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 属性系统。 对于应用程序中使用的每个QObject 子类,都会创建一个QMetaObject实例,该实例存储该QObject 子类的所有元信息。可以通过QObject::metaObject()访问该对象。
该类通常在应用程序编程中并非必需,但在编写元应用程序(如脚本引擎或 GUI 构建器)时非常有用。
您最可能用得上的函数包括:
- className() 返回类的名称。
- superClass() 返回父类的元对象。
- method() 和methodCount() 提供有关类元方法(信号、插槽及其他invokable 成员函数)的信息。
- enumerator() 和enumeratorCount() 提供有关类枚举器的信息。
- propertyCount() 和property() 提供有关类属性的信息。
- constructor() 和constructorCount() 提供有关类元构造函数的信息。
索引函数indexOfConstructor()、indexOfMethod()、indexOfEnumerator() 和indexOfProperty() 将构造函数、成员函数、枚举器或属性的名称映射到元对象中的索引。例如,当您将信号连接到槽时,Qt 会在内部使用indexOfMethod()。
类还可以包含一组名称-值对形式的附加类信息,这些信息存储在QMetaClassInfo 对象中。classInfoCount() 返回名称-值对的总数,classInfo() 返回单个名称-值对,而indexOfClassInfo() 则可用于查询名称-值对。
注意: 使用元对象系统的操作 通常是线程安全的,因为 QMetaObject 通常是在编译时生成的静态只读实例。但是,如果元对象被应用程序动态修改(例如,使用QQmlPropertyMap 时),则应用程序必须显式地对相关元对象的访问进行同步。
另请参阅 QMetaClassInfo 、QMetaEnum 、QMetaMethod 、QMetaProperty 、QMetaType 以及“元对象系统”。
成员函数文档
[static] bool QMetaObject::checkConnectArgs(const char *signal, const char *method)
如果参数 `signal ` 和 `method ` 兼容,则返回 `true `;否则返回 `false`。
signal 和method 均应为规范化形式。
另请参阅 normalizedSignature()。
[static] bool QMetaObject::checkConnectArgs(const QMetaMethod &signal, const QMetaMethod &method)
如果参数 `signal ` 和 `method ` 兼容,则返回 `true `;否则返回 `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, since 6.10] template <typename Functor> QMetaObject::Connection QMetaObject::connect(const QObject *sender, const QMetaMethod &signal, const QObject *context, Functor functor, Qt::ConnectionType type = Qt::AutoConnection)
创建一个连接,将sender 对象中signal 中的指定type 连接到functor ,并将其放置在context 的特定事件循环中。返回该连接的句柄,该句柄可用于后续断开连接。这对于将通过元对象内省获取的信号连接到捕获局部变量的lambda表达式非常有用。
注意:Qt ::UniqueConnections 不适用于 lambda 表达式、非成员函数和 functor;它们仅适用于成员函数。
槽函数可以是任何函数或 functor,其参数个数不得多于信号的参数个数。 对应的信号和槽参数必须完全匹配,该函数不处理隐式转换和类型检查。重载的 functor 需要借助 `qOverload` 显式解析。`signal ` 必须是信号的元方法,否则将返回无效的连接。
如果发送方或上下文被销毁,连接将自动断开。但是,您应确保在信号被发出时,函子中使用的任何对象仍然存活。
此函数重载了 QMetaObject::connect()。
注意:此函数是线程安全的。
该函数在 Qt 6.10 中引入。
另请参阅 QObject::connect() 和QObject::disconnect()。
[static, since 6.10] template <typename PointerToMemberFunction> QMetaObject::Connection QMetaObject::connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)
创建从sender 对象中的signal 到receiver 对象中的method 的连接,该连接使用指定的type 。返回该连接的句柄,该句柄可用于稍后断开连接。
如果无法建立连接(例如参数无效),则该 Connection 句柄将失效。您可以通过将其强制转换为 bool 类型来检查QMetaObject::Connection 是否有效。将返回的句柄传递给QObject::disconnect() 可断开连接。
如果信号的参数数量至少与插槽的参数数量相同,则该插槽可以连接到给定的信号。 对应的信号和槽参数必须完全匹配,本函数不处理隐式转换和类型检查。重载的槽需要借助qOverload 显式解析。signal 必须是信号的元方法,否则将返回无效连接。
本函数重载了QMetaObject::connect()。
注意:此函数是线程安全的。
该函数在 Qt 6.10 中引入。
另请参阅 QObject::connect() 和QObject::disconnect()。
[static] void QMetaObject::connectSlotsByName(QObject *object)
递归搜索给定object 的所有子对象,并将它们中匹配的信号连接到符合以下格式的object 的插槽上:
void on_<object name>_<signal name>(<signal parameters>);假设我们的对象有一个类型为QPushButton 的子对象,其object name 为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
如果该QMetaObject 所描述的类继承了metaObject 所描述的类型,则返回true ;否则返回false。
一种类型被视为继承自身。
[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)
在context 的事件循环中,使用arguments 调用function 。function 可以是 functor 或成员函数的指针。如果函数能够被调用,则返回true 。函数调用的返回值将存放在ret 中。用于ret 参数的对象应通过将您的对象传递给qReturnArg()来获取。例如:
MyClass *obj = ...;
int result = 0;
QMetaObject::invokeMethod(obj, &MyClass::myMethod, qReturnArg(result), parameter);如果设置了type ,则使用该连接类型调用函数。否则,将使用Qt::AutoConnection 。
注意:此函数是线程安全的。
这些函数在 Qt 6.7 中引入。
[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)
在context 的事件循环中调用function 。function 可以是 functor 或成员函数的指针。如果函数能够被调用,则返回true ;如果不存在该函数或参数不匹配,则返回false 。函数调用的返回值将被存入ret 中。
如果设置了type ,则使用该连接类型调用函数。否则,将使用Qt::AutoConnection 。
注意:此函数是线程安全的。
[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)
调用在元对象系统中注册的、针对对象obj 的方法,该方法通过名称member 进行标识。这包括槽、信号和Q_INVOKABLE 方法。如果该成员方法能够被调用,则返回true ;如果不存在该成员方法或参数不匹配,则返回false 。
对于带有 QTemplatedMetaMethodReturnArgument 参数的重载,member 函数调用的返回值将被放入ret 中。对于没有此类成员的重载,被调用函数的返回值(如有)将被丢弃。 QTemplatedMetaMethodReturnArgument 是一个内部类型,不应直接使用。应改用 qReturnArg() 函数。
带有 `Qt::ConnectionType `(type )参数的重载允许显式选择调用是否为同步:
- 若type 为Qt::DirectConnection ,则该成员将在当前线程中立即被调用。
- 如果type 为Qt::QueuedConnection ,则会发送一个QEvent ,并在应用程序进入obj 所创建或被移动到的线程的事件循环时立即调用该成员。
- 如果type 为Qt::BlockingQueuedConnection ,则该方法的调用方式与Qt::QueuedConnection 相同,不同之处在于当前线程将阻塞直至事件被传递。在同一线程内使用此连接类型进行对象间通信将导致死锁。
- 如果 `type ` 为 `Qt::AutoConnection`,且 `obj ` 与调用者位于同一线程中,则该成员函数将同步调用;否则将异步调用。这是不带 `type ` 参数的重载行为。
您只需将信号或槽的名称传递给此函数,无需传递完整的签名。例如,要异步调用QThread 上的quit() 槽,请使用以下代码:
QMetaObject::invokeMethod(thread, "quit", Qt::QueuedConnection);在异步方法调用中,参数必须是可复制类型,因为 Qt 需要在后台将参数复制并存储到事件中。 自 Qt 6.5 起,该函数会自动注册所使用的类型;但作为副作用,无法使用仅被前向声明的类型进行调用。此外,也无法进行使用非 const 修饰类型引用作为参数的异步调用。
若要对某个任意对象obj 同步调用compute(QString, int, double) 槽,并获取其返回值:
QString retVal;
QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection,
qReturnArg(retVal),
QString("sqrt"), 42, 9.7);如果“compute”槽方法接收的参数并非严格按指定顺序包含一个QString 、一个int 和一个double ,则调用将失败。 请注意,必须显式指定QString 的类型,因为字符字面量并非完全匹配的正确类型。如果该方法接受的参数分别是QStringView 、qsizetype 和float ,则调用应写为:
QString retVal;
QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection,
qReturnArg(retVal),
QStringView(u"sqrt"), qsizetype(42), 9.7f);也可以使用Q_ARG() 和Q_RETURN_ARG() 宏来执行相同的调用,如下所示:
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));保留这些宏是为了与 Qt 6.4 及更早版本保持兼容性,且可自由与未使用宏的参数混合使用。在极少数情况下,当调用将 typedef 定义的类型用作参数或返回类型的方法时,这些宏可能是必要的。
注意:此函数是线程安全的。
这些函数在 Qt 6.5 中引入。
另请参阅 Q_ARG()、Q_RETURN_ARG() 以及QMetaMethod::invoke()。
[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 使用规范化的签名来判断两个给定的信号和槽是否兼容。规范化处理会将空白符减少到最低限度,在适当的情况下将“const”移至最前,从值类型中移除“const”,并将 const 引用替换为值。
另请参阅 checkConnectArgs() 和normalizedType()。
[static] QByteArray QMetaObject::normalizedType(const char *type)
对type 进行归一化处理。
有关 Qt 如何进行归一化的说明,请参阅QMetaObject::normalizedSignature()。
示例:
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)
该宏接受一个Type 和一个该类型的value ,并返回一个QMetaMethodArgument,该对象可作为Args &&... 参数传递给模板QMetaObject::invokeMethod()。
另请参阅 Q_RETURN_ARG()。
QMetaMethodReturnArgument Q_RETURN_ARG(Type, Type &value)
该宏接受一个Type 对象以及该类型value 对象的非const引用,并返回一个QMetaMethodReturnArgument对象,该对象可作为Args &&... 参数传递给模板QMetaObject::invokeMethod()。
另请参阅 Q_ARG()。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.