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) |
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元对象系统(QtMeta-Object System)负责对象间的信号和插槽通信机制、运行时类型信息以及 Qt 属性系统。应用程序中使用的每个QObject 子类都会创建一个 QMetaObject 实例,该实例存储了QObject 子类的所有元信息。该对象以QObject::metaObject() 的形式提供。
应用程序编程通常不需要该类,但如果要编写元应用程序(如脚本引擎或图形用户界面构建器),该类则非常有用。
你最有可能发现有用的函数是这些:
- className() 返回类的名称。
- superClass() 返回超类的元对象。
- method() 和methodCount() 提供类的元方法(信号、槽和其他invokable 成员函数)信息。
- enumerator() 和enumeratorCount() 提供了类的枚举器信息。
- propertyCount() 和property() 提供了类的属性信息。
- constructor() 和constructorCount() 提供类的元构造函数信息。
索引函数indexOfConstructor()、indexOfMethod()、indexOfEnumerator() 和indexOfProperty() 将构造函数、成员函数、枚举器或属性的名称映射到元对象中的索引。例如,当您将信号连接到插槽时,Qt XML 在内部使用indexOfMethod() 。
类还可以有一个包含额外类信息的名-值对列表,存储在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)
在对象obj 上调用member (信号或槽名)。如果可以调用该成员,则返回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 起,该函数会自动注册正在使用的类型;但作为副作用,无法使用仅向前声明的类型进行调用。此外,异步调用也不能使用对非定式类型的引用作为参数。
要在某个任意对象上同步调用compute(QString, int, double)
槽,obj
获取其返回值:
QString retVal; QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection, qReturnArg(retVal), QString("sqrt"), 42, 9.7);
如果 "计算 "槽没有按照指定的顺序接收一个QString 、一个int
和一个double
,调用将失败。请注意有必要明确说明QString 的类型,因为字符字面量并不是匹配的正确类型。如果该方法使用的是QStringView 、qsizetype 和float
,则需要将调用写成:
QString retVal; QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection, qReturnArg(retVal), QStringView("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 及更早版本兼容,可以与不使用宏的参数自由混合。在极少数情况下,当调用一个使用类型定义的方法时,它们可能是必要的,因为该方法将类型定义为前向声明的类型作为参数或返回类型。
注意:此函数是线程安全的。
此函数在 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)
调用context 事件循环中的function 。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)
在context 的事件循环中通过arguments 调用function 。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)
如果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]
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。
类型被视为继承自身。
[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 XML 如何规范化的描述,请参阅QMetaObject::normalizedSignature() 。
示例:
QByteArray normType = QMetaObject::normalizedType(" int const *"); // normType is now "const int*"
另请参阅 normalizedSignature() 。
QMetaProperty QMetaObject::property(int index) const
返回具有给定index 的属性的元数据。如果不存在该属性,则返回空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,该 QMetaMethodArgument 可与Args &&...
参数一起传递给模板QMetaObject::invokeMethod() 。
另请参见 Q_RETURN_ARG()。
QMetaMethodReturnArgument Q_RETURN_ARG(Type, Type &value)
该宏接收Type 和指向该类型value 的非 Const 引用,并返回一个 QMetaMethodReturnArgument,该 QMetaMethodReturnArgument 可与Args &&...
参数一起传递给模板QMetaObject::invokeMethod() 。
另请参阅 Q_ARG().
© 2025 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.