[英]How to use QList to keep track of instances of child classes
I have two classes, Parent
and Child
(the latter is derived from the former) , and these are both QObject derived classes. 我有两个类, Parent
和Child
(后者是从前者派生的),它们都是QObject派生的类。 These classes are implemented as nodes on a tree structure, giving each node its specific functionality. 这些类被实现为树结构上的节点,从而为每个节点提供特定的功能。 Under the Parent
class there is a signal that creates a new object of type Child
whenever triggered. 在Parent
类下,有一个信号会在每次触发时创建一个Child
类型的新对象。 So after three triggers the tree structure would look like this: 因此,在三个触发器之后,树结构将如下所示:
PARENT 家长
---------Child 1 ---------儿童1
---------Child (1) ---------孩子(1)
---------Child (2) ---------孩子(2)
How would I use QList
to keep track of the number of child objects created? 我将如何使用QList
跟踪创建的子对象的数量? I want to append the index number with the name so that Child (1)
would look like Child 1
, it looks like a copy now. 我想在索引号后面加上名称,以使Child (1)
看起来像Child 1
,现在看起来像一个副本。
I have read the QList
documentation and I understand how to extract meaningful information once the objects are in a list, but it's this part that I can't find an answer to. 我已经阅读了QList
文档,并且了解了将对象放入列表后如何提取有意义的信息,但这是我找不到答案的部分。
EDIT: 编辑:
Say I did QList<Child*>ListID
, would this just initialise ListID
as a pointer to a list of type Parent
, or would it also populate that list? 说我做了QList<Child*>ListID
,这只是将ListID
初始化为一个指向Parent
类型列表的指针,还是会填充该列表?
Any suggestions? 有什么建议么?
PS: I wanted to know this before I started implementation, as I want to know if I am going about it in the wrong way. PS:在开始实施之前,我想知道这一点,因为我想知道我是否以错误的方式进行操作。 That is why I have no code to show. 这就是为什么我没有要显示的代码。 I was hoping for more of a casual discussion. 我希望有更多的随意讨论。
A bare QObject
itself is a tree node. 裸QObject
本身就是树节点。 Yes, a QObject
is a container of QObject
s! 是的,一个QObject
是容器QObject
小号! So, you need to do nothing special at all, since it keeps a list of all of its direct children, and also provides a way to recursively get all children of a particular type or name. 因此,您不需要做任何特别的事情,因为它保留了其所有直接子代的列表,并且还提供了一种递归获取所有特定类型或名称的子代的方法。
To access the list of children, use QObject::children()
. 要访问子列表,请使用QObject::children()
。 To get only the children of a specific type, use QObject::findChildren()
. 要仅获取特定类型的子代,请使用QObject::findChildren()
。
In your case, you can invoke auto list = findChildren<Child*>({}, Qt::FindDirectChildrenOnly)
to get the list when you need it. 在您的情况下,您可以在需要时调用auto list = findChildren<Child*>({}, Qt::FindDirectChildrenOnly)
来获取列表。 If you care not to dynamically allocate any memory, use QObject
's internal list directly: 如果您不希望动态分配任何内存,请直接使用QObject
的内部列表:
for (auto objChild : std::as_const(children()))
if (auto child = qobject_cast<Child*>(objChild)) {
...
}
Pre C++-17, use qAsConst
instead of std::as_const
. 在C ++-17之前的版本中,请使用qAsConst
代替std::as_const
。
If you're sure that only objects of a particular type are children, you can use a static_cast
instead and save a tiny bit of runtime: 如果确定只有特定类型的对象是子对象,则可以改用static_cast
并节省一点时间:
for (auto objChild : std::as_const(children())) {
auto child = static_cast<Child*>(objChild);
...
}
Under the
Parent
class there is a signal that creates a new object of typeChild
whenever triggered. 在Parent
类下,有一个信号会在每次触发时创建一个Child
类型的新对象。
Presumably you meant a slot? 想必您是说插槽吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.