QMetaObject 結構

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
(從 6.2 起) QMetaType metaType () const
QMetaMethod method (int index ) const
int methodCount () const
int methodOffset () const
(從 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 )
(從 6.10 起) QMetaObject::Connection connect (const QObject * sender , const QMetaMethod & signal , const QObject * context , Functor functor , Qt::ConnectionType type = Qt::AutoConnection)
(從 6.10 起) QMetaObject::Connection connect (const QObject * sender , const QMetaMethod & signal , const QObject * receiver , PointerToMemberFunction method , Qt::ConnectionType type = Qt::AutoConnection)
void connectSlotsByName (QObject * object )
(從 6.7 起) bool invokeMethod (QObject * context , Functor && function , Args &&... arguments )
bool invokeMethod (QObject * context , Functor && function , FunctorReturnType * ret )
(從 6.5 起) bool invokeMethod (QObject * obj , const char * member , Args &&... args )
(從 6.7 起) bool invokeMethod (QObject * context , Functor && function , QTemplatedMetaMethodReturnArgument<FunctorReturnType> ret , Args &&... arguments )
(從 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)
(從 6.5 起) bool invokeMethod (QObject * obj , const char * member , QTemplatedMetaMethodReturnArgument<ReturnArg> ret , Args &&... args )
(從 6.5 起) bool invokeMethod (QObject * obj , const char * member , Qt::ConnectionType type , Args &&... args )
(從 6.7 起) bool invokeMethod (QObject * context , Functor && function , Qt::ConnectionType type , QTemplatedMetaMethodReturnArgument<FunctorReturnType> ret , Args &&... arguments )
(從 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 負責信號/槽對象間的通信機製、運行時類型信息、及 Qt 特性係統。創建單 QMetaObject 實例為每個 QObject 在應用程序中使用的子類,且此實例存儲所有元信息為 QObject 子類。此對象是可用的作為 QObject::metaObject ().

應用程序編程通常不要求此類,但它很有用若編寫元應用程 (譬如:腳本引擎或 GUI 構建器)。

最可能找到的有用函數是這些:

索引函數 indexOfConstructor (), indexOfMethod (), indexOfEnumerator (),和 indexOfProperty () 將構造函數名稱、成員函數、枚舉器或特性映射到元對象中的索引。例如,Qt 使用 indexOfMethod () 在內部當把信號連接到槽時。

類也可以擁有列錶化的 namevalue 對的額外類信息,存儲在 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 )

援引 member (信號或槽名稱) 在對象 obj 。返迴 true 若成員可以被援引。返迴 false 若沒有這樣的成員或參數不匹配。

對於具有 QTemplatedMetaMethodReturnArgument 參數的重載,返迴值對於 member 函數調用被放置在 ret . For the overloads without such a member, the return value of the called function (if any) will be discarded. QTemplatedMetaMethodReturnArgument is an internal type you should not use directly. Instead, use the qReturnArg() function.

重載采用 Qt::ConnectionType 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 event loop in the thread that the obj 在其中被創建或被移動到。
  • 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. This is the behavior of the overloads that do not have the type 參數。

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 copyable types, because Qt needs to copy the arguments to store them in an event behind the scenes. Since Qt 6.5, this function automatically registers the types being used; however, as a side-effect, it is not possible to make calls using types that are only forward-declared. Additionally, it is not possible to make asynchronous calls that use references to non-const-qualified types as parameters either.

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

QString retVal;
QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection,
                         qReturnArg(retVal),
                         QString("sqrt"), 42, 9.7);
					

若 compute 槽不接受 1 個準確 QString , one int ,和 1 個 double in the specified order, the call will fail. Note how it was necessary to be explicit about the type of the QString , as the character literal is not exactly the right type to match. If the method instead took a QStringView qsizetype ,和 float , the call would need to be written as:

QString retVal;
QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection,
                         qReturnArg(retVal),
                         QStringView("sqrt"), qsizetype(42), 9.7f);
					

The same call can be executed using the Q_ARG () 和 Q_RETURN_ARG () macros, as in:

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

The macros are kept for compatibility with Qt 6.4 and earlier versions, and can be freely mixed with parameters that do not use the macro. They may be necessary in rare situations when calling a method that used a typedef to forward-declared type as a parameter or the return type.

注意: 此函數是 綫程安全 .

這些函數在 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)

援引 function 在事件循環的 context . 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 )

援引 function with arguments 在事件循環的 context . 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 )

返迴 truesignal and method 自變量兼容;否則返迴 false .

Both signal and method 期望被規範化。

另請參閱 normalizedSignature ().

[static] bool QMetaObject:: checkConnectArgs (const QMetaMethod & signal , const QMetaMethod & method )

返迴 truesignal and method 自變量兼容;否則返迴 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, since 6.10] template <typename Functor> QMetaObject::Connection QMetaObject:: connect (const QObject * sender , const QMetaMethod & signal , const QObject * context , Functor functor , Qt::ConnectionType type = Qt::AutoConnection)

創建連接按給定 type from signal in sender 對象到 functor 以放置在特定事件循環 context 。返迴連接的句柄,稍後可以使用它來斷開連接。這可以很有用,對於要把檢索自元對象自省的信號連接到 Lambda 捕獲局部變量。

注意: Qt::UniqueConnections 不工作對於 Lambda、非成員函數及函子;它們隻適用於成員函數。

槽函數可以是自變量數 <= 信號的任何函數 (或函子)。相應信號和槽之間的自變量必須準確匹配,此函數不處理隱式轉換和類型校驗。需要明確解析重載函子藉助 qOverload . signal 需要是信號的元方法,否則將返迴無效連接。

連接將自動斷開,若發送者 (或上下文) 被銷毀。不管怎樣,應小心在函子中使用的任何對象仍存活,當發射信號時。

此函數重載 QMetaObject::connect()。

注意: 此函數是 綫程安全 .

該函數在 Qt 6.10 引入。

另請參閱 QObject::connect () 和 QObject::disconnect ().

[static, since 6.10] template <typename PointerToMemberFunction> QMetaObject::Connection QMetaObject:: connect (const QObject * sender , const QMetaMethod & signal , const QObject * receiver , PointerToMemberFunction method , Qt::ConnectionType type = Qt::AutoConnection)

創建連接為給定 typesignalsender 對象到 methodreceiver 對象。返迴可用於稍後斷開連接的連接句柄。

連接句柄將無效 (例如:參數無效),若它無法創建連接。可以校驗是否 QMetaObject::Connection 有效通過把它鑄造成 bool。把返迴的句柄傳遞給 QObject::disconnect () 來斷開連接連接。

可以把槽連接到給定信號,若信號至少擁有如槽一樣多的自變量。相應信號和槽之間的自變量必須準確匹配,此函數不處理隱式轉換和類型校驗。需要明確解析重載槽藉助 qOverload . signal 需要是信號的元方法,否則將返迴無效連接。

此函數重載 QMetaObject::connect ().

注意: 此函數是 綫程安全 .

該函數在 Qt 6.10 引入。

另請參閱 QObject::connect () 和 QObject::disconnect ().

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

另請參閱 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

返迴 true 若類描述通過此 QMetaObject 繼承類型描述通過 metaObject ;否則返迴 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 使用規範化簽名決定 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*"
					

另請參閱 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 擁有 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 )

此宏接受 Typevalue 對於該類型並返迴 QMetaMethodArgument,可以將其傳遞給模闆 QMetaObject::invokeMethod () 采用 Args &&... 自變量。

另請參閱 Q_RETURN_ARG ().

QMetaMethodReturnArgument Q_RETURN_ARG ( Type , Type & value )

此宏接受 Type 和非常量引用為 value 對於該類型並返迴 QMetaMethodReturnArgument,可以將其傳遞給模闆 QMetaObject::invokeMethod () 采用 Args &&... 自變量。

另請參閱 Q_ARG ().