QMetaObject 类

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

头: #include <QMetaObject>

公共函数

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
QMetaMethod method (int index ) const
int methodCount () const
int methodOffset () const
QObject * newInstance (QGenericArgument val0 = QGenericArgument( 0 ), 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

静态公共成员

bool checkConnectArgs (const char * signal , const char * method )
void connectSlotsByName (QObject * object )
bool invokeMethod (QObject * obj , const char * member , Qt::ConnectionType type , QGenericReturnArgument ret , QGenericArgument val0 = QGenericArgument( 0 ), 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( 0 ), 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 , Qt::ConnectionType type , QGenericArgument val0 = QGenericArgument( 0 ), 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 , QGenericArgument val0 = QGenericArgument( 0 ), 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())
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 ().

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

成员函数文档编制

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

返回 true 若 signal and method arguments are compatible; otherwise returns false.

Both signal and method 期望被规范化。

另请参阅 normalizedSignature ().

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

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

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

若类没有带枚举的超类,偏移为 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 ().

该函数在 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 (),除了它会返回 -1,若方法存在但不是信号。

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

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

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

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

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

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

[static] bool QMetaObject:: invokeMethod ( QObject * obj , const char * member , Qt::ConnectionType type , QGenericReturnArgument ret , QGenericArgument val0 = QGenericArgument( 0 ), 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 . Returns true if the member could be invoked. Returns false if there is no such member or the parameters did not match.

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

返回值对于 member 函数调用被放置在 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 animateClick() slot on a QPushButton ,使用以下代码:

QMetaObject::invokeMethod(pushButton, "animateClick",
                          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 () 以在调用 invokeMethod() 之前注册数据类型。

要同步援引 compute(QString, int, double) 槽在某些任意对象 obj 检索其返回值:

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

若 compute 槽不接受 1 个准确 QString , one int and one double in the specified order, the call will fail.

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

[static] bool QMetaObject:: invokeMethod ( QObject * obj , const char * member , QGenericReturnArgument ret , QGenericArgument val0 = QGenericArgument( 0 ), 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())

此函数重载 invokeMethod ().

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

[static] bool QMetaObject:: invokeMethod ( QObject * obj , const char * member , Qt::ConnectionType type , QGenericArgument val0 = QGenericArgument( 0 ), 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())

此函数重载 invokeMethod ().

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

[static] bool QMetaObject:: invokeMethod ( QObject * obj , const char * member , QGenericArgument val0 = QGenericArgument( 0 ), 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())

此函数重载 invokeMethod ().

此重载援引成员使用连接类型 Qt::AutoConnection 并忽略返回值。

QMetaMethod QMetaObject:: method ( int index ) const

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

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

int QMetaObject:: methodCount () const

Returns the number of methods known to the meta-object system in this class, including the number of properties provided by each base class. These include signals and slots as well as member functions declared with the Q_INVOKABLE 宏。

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

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

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

int QMetaObject:: methodOffset () const

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

偏移是类的超类所有方法的和 (始终正值,因为 QObject has the deleteLater() slot and a destroyed() signal).

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

QObject * QMetaObject:: newInstance ( QGenericArgument val0 = QGenericArgument( 0 ), 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.

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

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

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

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

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

返回特性拥有 USER 标志被设为 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 和非常量引用为 value of that type and returns a QGenericReturnArgument 对象,其可以被传递给 QMetaObject::invokeMethod ().

另请参阅 Q_ARG ().