Aggregation::Aggregate Class
class Aggregation::AggregateAggregate 类定义了一组可视为一个整体的相关组件。更多内容...
| Header: | #include <aggregation/aggregate.h> |
| Inherits: | QObject |
注意:该类中的所有函数均是线程安全的。
公共函数
| Aggregate(QObject *parent = nullptr) | |
| virtual | ~Aggregate() override |
| void | add(QObject *component) |
| T * | component() |
| QList<T *> | components() |
| void | remove(QObject *component) |
信号
| void | changed() |
静态公共成员
| Aggregation::Aggregate * | parentAggregate(QObject *obj) |
相关的非成员
详细说明
聚合是一组被作为一个整体处理的组件,使得每个组件都能向外部暴露聚合中其他组件的属性和行为。具体来说,这意味着:
组件可以是任何从QObject 派生的类型。
您可以通过聚合的方式使用聚合体来模拟多重继承。假设我们有以下代码:
using namespace Aggregation; class MyInterface : public QObject { ........ }; class MyInterfaceEx : public QObject { ........ }; [...] MyInterface *object = new MyInterface; // this is single inheritance
该查询函数的作用类似于针对普通对象的 `qobject_cast()`:
Q_ASSERT(query<MyInterface>(object) == object); Q_ASSERT(query<MyInterfaceEx>(object) == 0);
如果我们希望object 同时实现MyInterfaceEx 类,但又不愿或无法使用多重继承,则可在任何环节通过聚合实现:
MyInterfaceEx *objectEx = new MyInterfaceEx; Aggregate::aggregate({object, objectEx})
聚合将这两个对象捆绑在一起。只要获取集合中的任意一部分,就能得到所有部分:
Q_ASSERT(query<MyInterface>(object) == object); Q_ASSERT(query<MyInterfaceEx>(object) == objectEx); Q_ASSERT(query<MyInterface>(objectEx) == object); Q_ASSERT(query<MyInterfaceEx>(objectEx) == objectEx);
以下代码将删除所有三个对象:object 、objectEx 和aggregate :
delete objectEx; // or delete object; // or delete aggregate;
支持聚合的代码绝不会使用qobject_cast()。它始终使用Aggregation::query(),该方法会像qobject_cast() 一样作为后备方案。
成员函数文档
Aggregate::Aggregate(QObject *parent = nullptr)
使用给定的parent 创建一个新的聚合。父对象会直接传递给该类的QObject 方法,除此之外不再被使用。
[override virtual noexcept] Aggregate::~Aggregate()
删除聚合时,会自动删除其所有组成部分。
void Aggregate::add(QObject *component)
将component 添加到聚合中。您无法添加属于其他聚合的组件,也无法添加聚合本身。
另请参阅 remove()。
[signal] void Aggregate::changed()
当组件被添加到聚合体中或从聚合体中移除时,会发出此信号。
template <typename T> T *Aggregate::component()
一个模板函数,用于返回指定类型的组件(如果存在)。如果存在多个该类型的组件,则随机返回其中一个。
另请参阅 Aggregate::components() 和add()。
template <typename T> QList<T *> Aggregate::components()
一个模板函数,用于返回所有符合给定类型(如有)的组件。
另请参见 Aggregate::component() 和add()。
[static] Aggregation::Aggregate *Aggregate::parentAggregate(QObject *obj)
如果存在obj 的聚合对象,则返回该对象;否则返回0。
void Aggregate::remove(QObject *component)
从聚合中移除component 。
另请参阅 add()。
相关非成员
template <typename T> T *query(QObject *obj)
执行一种动态类型转换,该转换会考虑 `obj ` 可能所属的聚合类型。如果 `obj ` 本身就是请求的类型,则直接进行类型转换并返回。否则,如果 `obj ` 属于某个聚合类型,则会检查其所有组成部分;如果它不属于任何聚合类型,则返回 `null`。
另请参阅 Aggregate::component()。
template <typename T> QList<T *> query_all(QObject *obj)
如果obj 属于聚合类型,则返回所有可转换为给定类型的分量。否则,如果它属于请求的类型,则返回obj 。
另请参阅 Aggregate::components()。
Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.