QMetaObject 类

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

头: #include <QMetaObject>
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
QMetaMethod method (int index ) const
int methodCount () const
int methodOffset () const
QObject * newInstance (QGenericArgument val0 = QGenericArgument(nullptr), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const
QMetaProperty property (int index ) const
int propertyCount () const
int propertyOffset () const
const QMetaObject * superClass () const
QMetaProperty userProperty () const

公共变量

struct (anonymous struct at /tmp/snapshot-qt5-5.12-rel/qt5/qtbase/include/QtCore/../../src/corelib/kernel/qobjectdefs.h:575:5) d

静态公共成员

bool checkConnectArgs (const char * signal , const char * method )
bool checkConnectArgs (const QMetaMethod & signal , const QMetaMethod & method )
void connectSlotsByName (QObject * object )
bool invokeMethod (QObject * obj , const char * member , Qt::ConnectionType type , QGenericReturnArgument ret , QGenericArgument val0 = QGenericArgument(nullptr), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument())
bool invokeMethod (QObject * obj , const char * member , QGenericReturnArgument ret , QGenericArgument val0 = ..., QGenericArgument val1 = ..., QGenericArgument val2 = ..., QGenericArgument val3 = ..., QGenericArgument val4 = ..., QGenericArgument val5 = ..., QGenericArgument val6 = ..., QGenericArgument val7 = ..., QGenericArgument val8 = ..., QGenericArgument val9 = ...)
bool invokeMethod (QObject * obj , const char * member , Qt::ConnectionType type , QGenericArgument val0 = ..., QGenericArgument val1 = ..., QGenericArgument val2 = ..., QGenericArgument val3 = ..., QGenericArgument val4 = ..., QGenericArgument val5 = ..., QGenericArgument val6 = ..., QGenericArgument val7 = ..., QGenericArgument val8 = ..., QGenericArgument val9 = ...)
bool invokeMethod (QObject * obj , const char * member , QGenericArgument val0 = ..., QGenericArgument val1 = ..., QGenericArgument val2 = ..., QGenericArgument val3 = ..., QGenericArgument val4 = ..., QGenericArgument val5 = ..., QGenericArgument val6 = ..., QGenericArgument val7 = ..., QGenericArgument val8 = ..., QGenericArgument val9 = ...)
bool invokeMethod (QObject * context , Functor function , Qt::ConnectionType type = Qt::AutoConnection, FunctorReturnType * ret = nullptr)
bool invokeMethod (QObject * context , Functor function , FunctorReturnType * ret )
QByteArray normalizedSignature (const char * method )
QByteArray normalizedType (const char * type )

QGenericArgument Q_ARG ( Type , const Type & value )
QGenericReturnArgument Q_RETURN_ARG ( Type , Type & value )

详细描述

The QMetaObject 类包含有关 Qt 对象的元信息。

Qt 元对象系统 in Qt is responsible for the signals and slots inter-object communication mechanism, runtime type information, and the Qt property system. A single QMetaObject instance is created for each QObject 在应用程序中使用的子类,且此实例存储所有元信息为 QObject 子类。此对象是可用的作为 QObject::metaObject ().

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

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

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

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

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

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

成员函数文档编制

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

该函数在 Qt 5.0 引入。

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

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.

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

该函数在 Qt 4.5 引入。

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

int QMetaObject:: constructorCount () const

返回此类的构造函数数。

该函数在 Qt 4.5 引入。

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

QMetaEnum QMetaObject:: enumerator ( int index ) const

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

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

int QMetaObject:: enumeratorCount () const

返回此类中的枚举器数。

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

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

该函数在 Qt 4.5 引入。

另请参阅 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 (), except that it will return -1 if the method exists but isn't a signal.

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

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

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

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

这如同 indexOfMethod (), except that it will return -1 if the method exists but isn't a slot.

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

bool QMetaObject:: inherits (const QMetaObject * metaObject ) const

返回 true if the class described by this QMetaObject inherits the type described by metaObject ;否则返回 false。

类型被认为继承本身。

该函数在 Qt 5.7 引入。

[static] bool QMetaObject:: invokeMethod ( QObject * obj , const char * member , Qt::ConnectionType type , QGenericReturnArgument ret , QGenericArgument val0 = QGenericArgument(nullptr), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument())

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

援引可以是同步 (或异步) 的,从属 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 main event loop.
  • 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.

The return value of the member function call is placed in ret . If the invocation is asynchronous, the return value cannot be evaluated. You can pass up to ten arguments ( val0 , val1 , val2 , val3 , val4 , val5 , val6 , val7 , val8 ,和 val9 ) to the member 函数。

QGenericArgument and QGenericReturnArgument are internal helper classes. Because signals and slots can be dynamically invoked, you must enclose the arguments using the Q_ARG () 和 Q_RETURN_ARG () 宏。 Q_ARG () takes a type name and a const reference of that type; Q_RETURN_ARG () takes a type name and a non-const reference.

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 of types that are known to Qt's meta-object system, because Qt needs to copy the arguments to store them in an event behind the scenes. If you try to use a queued connection and get the error message

QMetaObject::invokeMethod: Unable to handle unregistered datatype 'MyType'
					

call qRegisterMetaType () to register the data type before you call invokeMethod().

要同步援引 compute(QString, int, double) slot on some arbitrary object obj retrieve its return value:

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

If the "compute" slot does not take exactly one QString , one int and one double in the specified order, the call will fail.

注意: 此函数是 thread-safe .

另请参阅 Q_ARG (), Q_RETURN_ARG (), qRegisterMetaType (),和 QMetaMethod::invoke ().

[static] bool QMetaObject:: invokeMethod ( QObject * obj , const char * member , QGenericReturnArgument ret , QGenericArgument val0 = ..., QGenericArgument val1 = ..., QGenericArgument val2 = ..., QGenericArgument val3 = ..., QGenericArgument val4 = ..., QGenericArgument val5 = ..., QGenericArgument val6 = ..., QGenericArgument val7 = ..., QGenericArgument val8 = ..., QGenericArgument val9 = ...)

此函数重载 invokeMethod ().

此重载总是援引成员使用连接类型 Qt::AutoConnection .

注意: 此函数是 thread-safe .

[static] bool QMetaObject:: invokeMethod ( QObject * obj , const char * member , Qt::ConnectionType type , QGenericArgument val0 = ..., QGenericArgument val1 = ..., QGenericArgument val2 = ..., QGenericArgument val3 = ..., QGenericArgument val4 = ..., QGenericArgument val5 = ..., QGenericArgument val6 = ..., QGenericArgument val7 = ..., QGenericArgument val8 = ..., QGenericArgument val9 = ...)

此函数重载 invokeMethod ().

此重载可以被使用,若对成员的返回值不感兴趣。

注意: 此函数是 thread-safe .

[static] bool QMetaObject:: invokeMethod ( QObject * obj , const char * member , QGenericArgument val0 = ..., QGenericArgument val1 = ..., QGenericArgument val2 = ..., QGenericArgument val3 = ..., QGenericArgument val4 = ..., QGenericArgument val5 = ..., QGenericArgument val6 = ..., QGenericArgument val7 = ..., QGenericArgument val8 = ..., QGenericArgument val9 = ...)

此函数重载 invokeMethod ().

This overload invokes the member using the connection type Qt::AutoConnection 并忽略返回值。

注意: 此函数是 thread-safe .

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

这是重载函数。

援引 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 .

注意: 此函数是 thread-safe .

该函数在 Qt 5.10 引入。

[static] bool QMetaObject:: invokeMethod ( QObject * context , Functor function , FunctorReturnType * ret )

这是重载函数。

援引 function in the event loop of context using the connection type Qt::AutoConnection . 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 member or the parameters did not match. The return value of the function call is placed in ret .

注意: 此函数是 thread-safe .

该函数在 Qt 5.10 引入。

QMetaMethod QMetaObject:: method ( int index ) const

Returns the meta-data for the method with the given 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 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());
					

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

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

QObject *QMetaObject:: newInstance ( QGenericArgument val0 = QGenericArgument(nullptr), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const

Constructs a new instance of this class. You can pass up to ten arguments ( val0 , val1 , val2 , val3 , val4 , val5 , val6 , val7 , val8 ,和 val9 ) to the constructor. Returns the new object, or 0 if no suitable constructor is available.

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

该函数在 Qt 4.5 引入。

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

该函数在 Qt 4.2 引入。

另请参阅 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 has the name() property).

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

const QMetaObject *QMetaObject:: superClass () const

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

另请参阅 className ().

QMetaProperty QMetaObject:: userProperty () const

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

该函数在 Qt 4.2 引入。

另请参阅 QMetaProperty::isUser ().

宏文档编制

QGenericArgument Q_ARG ( Type , const Type & value )

此宏接受 Typevalue of that type and returns a QGenericArgument 对象,其可以被传递给 QMetaObject::invokeMethod ().

另请参阅 Q_RETURN_ARG ().

QGenericReturnArgument Q_RETURN_ARG ( Type , Type & value )

此宏接受 Type and a non-const reference to a value of that type and returns a QGenericReturnArgument 对象,其可以被传递给 QMetaObject::invokeMethod ().

另请参阅 Q_ARG ().