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 ) |
| 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 () 在內部當把信號連接到槽時。
類也可以擁有列錶化的 name – value 對的額外類信息,存儲在 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 參數允許明確選擇是否將同步援引:
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
)
返迴
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
.
返迴類信息項的元數據采用給定 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 ().
返迴此類類信息的項數。
另請參閱 classInfo (), classInfoOffset (),和 indexOfClassInfo ().
返迴用於此類的類信息偏移量;即:此類的第一類信息項的索引位置。
若類是沒有類信息的超類,偏移為 0;否則,偏移是類的超類的所有類信息項的和。
另請參閱 classInfo (), classInfoCount (),和 indexOfClassInfo ().
返迴類名。
另請參閱 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 ().
返迴構造函數的元數據采用給定 index .
另請參閱 constructorCount () 和 newInstance ().
返迴此類的構造函數數。
另請參閱 constructor () 和 indexOfConstructor ().
返迴枚舉器的元數據采用給定 index .
另請參閱 enumeratorCount (), enumeratorOffset (),和 indexOfEnumerator ().
返迴此類中的枚舉器數。
另請參閱 enumerator (), enumeratorOffset (),和 indexOfEnumerator ().
返迴用於此類的枚舉偏移;即:此類的第一枚舉器的索引位置。
若類沒有帶枚舉的超類,偏移為 0;否則,偏移是類的超類的所有枚舉的和。
另請參閱 enumerator (), enumeratorCount (),和 indexOfEnumerator ().
查找類信息項 name 並返迴其索引;否則返迴 -1。
另請參閱 classInfo (), classInfoCount (),和 classInfoOffset ().
查找 constructor 並返迴其索引;否則返迴 -1。
注意, constructor 必須是規範化形式,作為返迴通過 normalizedSignature ().
另請參閱 constructor (), constructorCount (),和 normalizedSignature ().
查找枚舉器 name 並返迴其索引;否則返迴 -1。
另請參閱 enumerator (), enumeratorCount (),和 enumeratorOffset ().
查找 method 並返迴其索引;否則返迴 -1。
注意, method 必須是規範化形式,作為返迴通過 normalizedSignature ().
另請參閱 method (), methodCount (), methodOffset (),和 normalizedSignature ().
查找特性 name 並返迴其索引;否則返迴 -1。
另請參閱 property (), propertyCount (),和 propertyOffset ().
查找 signal 並返迴其索引;否則返迴 -1。
這如同 indexOfMethod (),除瞭它會返迴 -1,若方法存在但不是信號。
注意, signal 必須是規範化形式,作為返迴通過 normalizedSignature ().
另請參閱 indexOfMethod (), normalizedSignature (), method (), methodCount (),和 methodOffset ().
查找 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 引入。
返迴方法的元數據采用給定 index .
另請參閱 methodCount (), methodOffset (),和 indexOfMethod ().
返迴此類的方法數,包括每個基類提供的方法數。這些包括信號-槽,及正常成員函數。
使用像以下代碼獲得的 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 ().
返迴此類的方法偏移;即:此類第一成員函數的索引位置。
偏移是類的超類所有方法的和 (始終正值,因為 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 ().
返迴特性的元數據采用給定 index 。若沒有這種特性存在,null QMetaProperty 被返迴。
另請參閱 propertyCount (), propertyOffset (),和 indexOfProperty ().
返迴此類的特性數,包括每個基類提供的特性數。
使用像以下代碼獲得的 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 ().
返迴此類的屬性偏移;即:此類第一特性的索引位置。
偏移是類的超類的所有特性的和 (始終正值,因為 QObject 擁有 objectName 特性)。
另請參閱 property (), propertyCount (),和 indexOfProperty ().
返迴超類的元對象,或
nullptr
若沒有這種對象。
另請參閱 className ().
返迴特性擁有
USER
標誌被設為 true。
另請參閱 QMetaProperty::isUser ().
此宏接受
Type
和
value
對於該類型並返迴 QMetaMethodArgument,可以將其傳遞給模闆
QMetaObject::invokeMethod
() 采用
Args &&...
自變量。
另請參閱 Q_RETURN_ARG ().
此宏接受
Type
和非常量引用為
value
對於該類型並返迴 QMetaMethodReturnArgument,可以將其傳遞給模闆
QMetaObject::invokeMethod
() 采用
Args &&...
自變量。
另請參閱 Q_ARG ().