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 | 方法 (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.5 起)
bool
|
invokeMethod (QObject * obj , const char * member , Qt::ConnectionType type , QTemplatedMetaMethodReturnArgument<ReturnArg> ret , Args &&... args ) |
(从 6.5 起)
bool
|
invokeMethod (QObject * obj , const char * member , Qt::ConnectionType type , Args &&... args ) |
(从 6.5 起)
bool
|
invokeMethod (QObject * obj , const char * member , QTemplatedMetaMethodReturnArgument<ReturnArg> ret , Args &&... args ) |
(从 6.5 起)
bool
|
invokeMethod (QObject * obj , const char * member , Args &&... args ) |
bool | invokeMethod (QObject * context , Functor && function , Qt::ConnectionType type = Qt::AutoConnection, FunctorReturnType * ret = nullptr) |
bool | invokeMethod (QObject * context , Functor && function , FunctorReturnType * ret ) |
(从 6.7 起)
bool
|
invokeMethod (QObject * context , Functor && function , Qt::ConnectionType type , QTemplatedMetaMethodReturnArgument<FunctorReturnType> ret , Args &&... arguments ) |
(从 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 ) |
(从 6.7 起)
bool
|
invokeMethod (QObject * context , Functor && function , Args &&... arguments ) |
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
若没有这样的成员或参数不匹配。
For the overloads with a QTemplatedMetaMethodReturnArgument parameter, the return value of the member function call is placed in 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.
The overloads with a Qt::ConnectionType type parameter allow explicitly selecting whether the invocation will be synchronous or not:
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)
slot on some arbitrary object
obj
检索其返回值:
QString retVal; QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection, qReturnArg(retVal), QString("sqrt"), 42, 9.7);
若 compute 槽不接受恰好一个
QString
, one
int
, and one
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.
注意: 此函数是 thread-safe .
该函数在 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
if there is no such function or the parameters did not match. The return value of the function call is placed in
ret
.
若 type is set, then the function is invoked using that connection type. Otherwise, Qt::AutoConnection 会被使用。
注意: 此函数是 thread-safe .
[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
if the function could be invoked. The return value of the function call is placed in
ret
. The object used for the
ret
argument should be obtained by passing your object to qReturnArg(). For example:
MyClass *obj = ...; int result = 0; QMetaObject::invokeMethod(obj, &MyClass::myMethod, qReturnArg(result), parameter);
若 type is set, then the function is invoked using that connection type. Otherwise, Qt::AutoConnection 会被使用。
注意: 此函数是 thread-safe .
该函数在 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 ().
返回用于此类的枚举偏移;即:此类的第一枚举器的索引位置。
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 ().
查找类信息项 name 并返回其索引;否则返回 -1。
另请参阅 classInfo (), classInfoCount (),和 classInfoOffset ().
查找 constructor 并返回其索引;否则返回 -1。
注意, constructor 必须是规范化形式,作为返回通过 normalizedSignature ().
另请参阅 constructor (), constructorCount (),和 normalizedSignature ().
查找枚举器 name 并返回其索引;否则返回 -1。
另请参阅 enumerator (), enumeratorCount (),和 enumeratorOffset ().
查找 method 并返回其索引;否则返回 -1。
注意, method 必须是规范化形式,作为返回通过 normalizedSignature ().
另请参阅 方法 (), methodCount (), methodOffset (),和 normalizedSignature ().
查找特性 name 并返回其索引;否则返回 -1。
另请参阅 property (), propertyCount (),和 propertyOffset ().
查找 signal 并返回其索引;否则返回 -1。
这如同 indexOfMethod (),除了它会返回 -1,若方法存在但不是信号。
注意, signal 必须是规范化形式,作为返回通过 normalizedSignature ().
另请参阅 indexOfMethod (), normalizedSignature (), 方法 (), methodCount (),和 methodOffset ().
查找 slot 并返回其索引;否则返回 -1。
这如同 indexOfMethod (),除了它会返回 -1,若方法存在但不是槽。
另请参阅 indexOfMethod (), 方法 (), methodCount (),和 methodOffset ().
[noexcept]
bool
QMetaObject::
inherits
(const
QMetaObject
*
metaObject
) const
返回
true
若类描述通过此
QMetaObject
继承类型描述通过
metaObject
;否则返回 false。
类型被认为继承本身。
[since 6.2]
QMetaType
QMetaObject::
metaType
() const
Returns the metatype corresponding to this metaobject. If the metaobject originates from a namespace, an invalid metatype is returned.
该函数在 Qt 6.2 引入。
返回用于方法的元数据按给定 index .
另请参阅 methodCount (), methodOffset (),和 indexOfMethod ().
Returns the number of methods in this class, including the number of methods provided by each base class. These include signals and slots as well as normal member functions.
使用像以下代码获得的 QStringList 包含给定类特定方法:
const QMetaObject* metaObject = obj->metaObject(); QStringList methods; for(int i = metaObject->methodOffset(); i < metaObject->methodCount(); ++i) methods << QString::fromLatin1(metaObject->method(i).methodSignature());
另请参阅 方法 (), methodOffset (),和 indexOfMethod ().
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 ().
[since 6.5]
template <typename... Args>
QObject
*QMetaObject::
newInstance
(
Args
&&...
arguments
) const
Constructs a new instance of this class and returns the new object, or
nullptr
if no suitable constructor is available. The types of the arguments
arguments
will be used to find a matching constructor, and then forwarded to it the same way signal-slot connections do.
Note that only constructors that are declared with the Q_INVOKABLE modifier are made available through the meta-object system.
该函数在 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 ().
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 拥有 name() 特性)。
另请参阅 property (), propertyCount (),和 indexOfProperty ().
返回超类的元对象,或
nullptr
若没有这种对象。
另请参阅 className ().
返回特性拥有
USER
标志被设为 true。
另请参阅 QMetaProperty::isUser ().
此宏接受
Type
和
value
of that type and returns a QMetaMethodArgument, which can be passed to the template
QMetaObject::invokeMethod
() 采用
Args &&...
自变量。
另请参阅 Q_RETURN_ARG ().
此宏接受
Type
and a non-const reference to a
value
of that type and returns a QMetaMethodReturnArgument, which can be passed to the template
QMetaObject::invokeMethod
() 采用
Args &&...
自变量。
另请参阅 Q_ARG ().