QObject* a;
QObject* b;
//a and b are created somewhere else
bool existed = b.contains(a);//check if b contains a recursively我想知道有没有什么API可以做到这一点?
发布于 2019-10-14 17:57:25
为此,您可以使用不带任何参数的QObject::findChildren:
如下所示:
bool exists = b->findChildren<QObject*>().contains(a);请注意,我认为你需要这样做的事实可能意味着设计错误,你应该重新考虑你试图以这种方式实现的东西,而这种攻击是不必要的。这很可能做不到,但不要一直做这种事情,因为它相当丑陋,如果你从一开始就依赖它,它可能会变得非常慢。
发布于 2019-10-14 17:51:18
而不是问“a是b的后代吗?”试着把它转过来,问“b是a的祖先吗?”然后只需重复调用QObject::parent即可。例如(未测试的)..。
bool is_ancestor_of (const QObject *descendant, const QObject *ancestor)
{
if (!ancestor)
return false;
while (descendant) {
if (descendant == ancestor)
return true;
descendant = descendant->parent();
}
return false;
}https://stackoverflow.com/questions/58373607
复制相似问题