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( Q_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

靜態公共成員

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( Q_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( Q_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 , Qt::ConnectionType type , QGenericArgument val0 = QGenericArgument( Q_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 , QGenericArgument val0 = QGenericArgument( Q_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())
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 自變量兼容;否則返迴 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

返迴用於此類的類信息偏移量;即:此類的第一類信息項的索引位置。

若類是沒有類信息的超類,偏移為 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();
					

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

返迴用於此類的枚舉偏移;即:此類的第一枚舉器的索引位置。

若類沒有帶枚舉的超類,偏移為 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 ().

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

返迴 true 若類描述通過此 QMetaObject 繼承類型描述通過 metaObject ;否則返迴 false。

類型被認為繼承本身。

該函數在 Qt 5.7 引入。

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

返迴值對於 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 quit() slot on a QThread ,使用以下代碼:

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 () 以在調用 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( Q_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())

此函數重載 invokeMethod ().

此重載總是援引成員使用連接類型 Qt::AutoConnection .

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

此函數重載 invokeMethod ().

此重載可以被使用,若對成員的返迴值不感興趣。

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

此函數重載 invokeMethod ().

此重載援引成員使用連接類型 Qt::AutoConnection 並忽略返迴值。

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 has the deleteLater() slot and a destroyed() signal).

另請參閱 method (), methodCount (),和 indexOfMethod ().

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

注意,僅構造函數的聲明采用 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 ().