QMetaObject Class Reference

[ QtCore module]

The QMetaObject class contains meta-information about Qt 对象。 更多...

方法

Static Methods


详细描述

The QMetaObject class contains meta-information about Qt 对象。

Qt Meta-Object 系统 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 subclass that is used in an application, and this instance stores all the meta-information for the QObject subclass. This object is available as QObject.metaObject ().

This class is not normally required for application programming, but it is useful if you write meta-applications, such as scripting engines or GUI builders.

The functions you are most likely to find useful are these:

The index functions indexOfConstructor (), indexOfMethod (), indexOfEnumerator (),和 indexOfProperty () map names of constructors, member functions, enumerators, or properties to indexes in the meta-object. For example, Qt uses indexOfMethod () internally when you connect a signal to a slot.

Classes can also have a list of name -- value pairs of additional class information, stored in QMetaClassInfo objects. The number of pairs is returned by classInfoCount (), single pairs are returned by classInfo (), and you can search for pairs with indexOfClassInfo ().


方法文档编制

QMetaObject.__init__ ( self )

QMetaObject.__init__ ( self , QMetaObject )

bool QMetaObject.checkConnectArgs (str  signal , str  方法 )

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

Both signal and 方法 are expected to be normalized.

另请参阅 normalizedSignature ().

QMetaClassInfo QMetaObject.classInfo ( self , int  index )

Returns the meta-data for the item of class information with the given 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 ( self )

Returns the number of items of class information in this 类。

另请参阅 classInfo (), classInfoOffset (),和 indexOfClassInfo ().

int QMetaObject.classInfoOffset ( self )

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

str QMetaObject.className ( self )

Returns the class name.

另请参阅 superClass ().

QMetaObject.connectSlotsByName ( QObject   o )

Searches recursively for all child objects of the given object , and connects matching signals from them to slots of object that follow the following form:

 void on_<object name>_<signal name>(<signal parameters>);
			

Let's assume our object has a child object of type QPushButton 采用 object name button1 . The slot to catch the button's clicked() signal would be:

 void on_button1_clicked();
			

另请参阅 QObject.setObjectName ().

QMetaMethod QMetaObject.constructor ( self , int  index )

Returns the meta-data for the constructor with the given index .

该函数在 Qt 4.5 引入。

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

int QMetaObject.constructorCount ( self )

返回此类中的构造函数数。

该函数在 Qt 4.5 引入。

另请参阅 构造函数 () 和 indexOfConstructor ().

QMetaEnum QMetaObject.enumerator ( self , int  index )

Returns the meta-data for the enumerator with the given index .

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

int QMetaObject.enumeratorCount ( self )

Returns the number of enumerators in this class.

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

int QMetaObject.enumeratorOffset ( self )

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 ( self , str  name )

Finds class information item name and returns its index; otherwise returns -1.

另请参阅 classInfo (), classInfoCount (),和 classInfoOffset ().

int QMetaObject.indexOfConstructor ( self , str  构造函数 )

查找 构造函数 and returns its index; otherwise returns -1.

注意: 构造函数 has to be in normalized form, as returned by normalizedSignature ().

该函数在 Qt 4.5 引入。

另请参阅 构造函数 (), constructorCount (),和 normalizedSignature ().

int QMetaObject.indexOfEnumerator ( self , str  name )

Finds enumerator name and returns its index; otherwise returns -1.

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

int QMetaObject.indexOfMethod ( self , str  方法 )

查找 方法 and returns its index; otherwise returns -1.

注意: 方法 has to be in normalized form, as returned by normalizedSignature ().

另请参阅 方法 (), methodCount (), methodOffset (),和 normalizedSignature ().

int QMetaObject.indexOfProperty ( self , str  name )

Finds property name and returns its index; otherwise returns -1.

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

int QMetaObject.indexOfSignal ( self , str  signal )

查找 signal and returns its index; otherwise returns -1.

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

注意: signal has to be in normalized form, as returned by normalizedSignature ().

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

int QMetaObject.indexOfSlot ( self , str  slot )

查找 slot and returns its index; otherwise returns -1.

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

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

object QMetaObject.invokeMethod ( QObject   obj , str  member , Qt.ConnectionType   type , QGenericReturnArgument   ret , QGenericArgument   value0  = QGenericArgument(0,0), QGenericArgument   value1  = QGenericArgument(0,0), QGenericArgument   value2  = QGenericArgument(0,0), QGenericArgument   value3  = QGenericArgument(0,0), QGenericArgument   value4  = QGenericArgument(0,0), QGenericArgument   value5  = QGenericArgument(0,0), QGenericArgument   value6  = QGenericArgument(0,0), QGenericArgument   value7  = QGenericArgument(0,0), QGenericArgument   value8  = QGenericArgument(0,0), QGenericArgument   value9  = QGenericArgument(0,0))

援引 member (a signal or a slot name) on the object obj . Returns true if the member could be invoked. Returns false if there is no such member or the parameters did not match.

The invocation can be either synchronous or asynchronous, depending on type :

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 () macros. 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 , use the following code:

 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 () 到 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.

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

object QMetaObject.invokeMethod ( QObject   obj , str  member , QGenericReturnArgument   ret , QGenericArgument   value0  = QGenericArgument(0,0), QGenericArgument   value1  = QGenericArgument(0,0), QGenericArgument   value2  = QGenericArgument(0,0), QGenericArgument   value3  = QGenericArgument(0,0), QGenericArgument   value4  = QGenericArgument(0,0), QGenericArgument   value5  = QGenericArgument(0,0), QGenericArgument   value6  = QGenericArgument(0,0), QGenericArgument   value7  = QGenericArgument(0,0), QGenericArgument   value8  = QGenericArgument(0,0), QGenericArgument   value9  = QGenericArgument(0,0))

此函数重载 invokeMethod ().

This overload always invokes the member using the connection type Qt.AutoConnection .

object QMetaObject.invokeMethod ( QObject   obj , str  member , Qt.ConnectionType   type , QGenericArgument   value0  = QGenericArgument(0,0), QGenericArgument   value1  = QGenericArgument(0,0), QGenericArgument   value2  = QGenericArgument(0,0), QGenericArgument   value3  = QGenericArgument(0,0), QGenericArgument   value4  = QGenericArgument(0,0), QGenericArgument   value5  = QGenericArgument(0,0), QGenericArgument   value6  = QGenericArgument(0,0), QGenericArgument   value7  = QGenericArgument(0,0), QGenericArgument   value8  = QGenericArgument(0,0), QGenericArgument   value9  = QGenericArgument(0,0))

此函数重载 invokeMethod ().

This overload can be used if the return value of the member is of no interest.

object QMetaObject.invokeMethod ( QObject   obj , str  member , QGenericArgument   value0  = QGenericArgument(0,0), QGenericArgument   value1  = QGenericArgument(0,0), QGenericArgument   value2  = QGenericArgument(0,0), QGenericArgument   value3  = QGenericArgument(0,0), QGenericArgument   value4  = QGenericArgument(0,0), QGenericArgument   value5  = QGenericArgument(0,0), QGenericArgument   value6  = QGenericArgument(0,0), QGenericArgument   value7  = QGenericArgument(0,0), QGenericArgument   value8  = QGenericArgument(0,0), QGenericArgument   value9  = QGenericArgument(0,0))

此函数重载 invokeMethod ().

This overload invokes the member using the connection type Qt.AutoConnection and ignores return values.

QMetaMethod QMetaObject.method ( self , int  index )

Returns the meta-data for the method with the given index .

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

int QMetaObject.methodCount ( self )

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 宏。

Use code like the following to obtain a 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).signature());
			

另请参阅 方法 (), methodOffset (),和 indexOfMethod ().

int QMetaObject.methodOffset ( self )

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

另请参阅 方法 (), methodCount (),和 indexOfMethod ().

QObject QMetaObject.newInstance ( self , QGenericArgument   value0  = QGenericArgument(0,0), QGenericArgument   value1  = QGenericArgument(0,0), QGenericArgument   value2  = QGenericArgument(0,0), QGenericArgument   value3  = QGenericArgument(0,0), QGenericArgument   value4  = QGenericArgument(0,0), QGenericArgument   value5  = QGenericArgument(0,0), QGenericArgument   value6  = QGenericArgument(0,0), QGenericArgument   value7  = QGenericArgument(0,0), QGenericArgument   value8  = QGenericArgument(0,0), QGenericArgument   value9  = QGenericArgument(0,0))

Constructs a new instance of this class. You can pass up to ten arguments ( val0 , val1 , val2 , val3 , val4 , val5 , val6 , val7 , val8 , and 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 () 和 构造函数 ().

QByteArray QMetaObject.normalizedSignature (str  方法 )

Normalizes the signature of the given 方法 .

Qt uses normalized signatures to decide whether two given signals and slots are compatible. Normalization reduces whitespace to a minimum, moves 'const' to the front where appropriate, removes 'const' from value types and replaces const references with 值。

另请参阅 checkConnectArgs () 和 normalizedType ().

QByteArray QMetaObject.normalizedType (str  type )

Normalizes a type .

QMetaObject.normalizedSignature () for a description on how Qt normalizes.

范例:

 QByteArray normType = QMetaObject.normalizedType(" int    const  *");
 // normType is now "const int*"
			

该函数在 Qt 4.2 引入。

另请参阅 normalizedSignature ().

QMetaProperty QMetaObject.property ( self , int  index )

Returns the meta-data for the property with the given index . If no such property exists, a null QMetaProperty 被返回。

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

int QMetaObject.propertyCount ( self )

Returns the number of properties in this class, including the number of properties provided by each base class.

Use code like the following to obtain a QStringList containing the properties specific to a given class:

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

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

QMetaObject QMetaObject.superClass ( self )

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

另请参阅 className ().

QMetaProperty QMetaObject.userProperty ( self )

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

该函数在 Qt 4.2 引入。

另请参阅 QMetaProperty.isUser ().