简体   繁体   English

如何应对儿童能见度的变化?

[英]How to react on a change of visibility of children?

I have different container items and I would like to make not visible when all of their children are not visible. 我有不同的容器项目,当所有子项都不可见时,我希望使其不可见。

Dynamically, some code is parsing all the items recursively and set them visible or not visible depending on some filter (like a search input). 动态地,一些代码递归地解析所有项目,并根据某些过滤器(例如搜索输入)将它们设置为可见或不可见。 The container items are ignored by this filtering. 该过滤将忽略容器项目。

After that filtering, how can I detect the visibility has changed and update my container items accordingly ? 过滤之后,如何检测可见性已更改并相应地更新容器项目?

So far I have this code but I need this to be executed every time the children visibility is changed : 到目前为止,我已经有了这段代码,但是每次子级可见性更改时,我都需要执行此代码:

visible: visibleChildren.size > 0

Try this instead: 尝试以下方法:

visible: visibleChildren.length > 0

In fact, only visible: visibleChildren.length will do as well. 实际上,只有visible: visibleChildren.length也可以。

visibleChildren.size is undefined - there is no size member. visibleChildren.size undefined -没有size成员。

Keep in mind you will have problems setting items back to visible, judging by the behavior of the following code: 请记住,根据以下代码的行为,将项目设置回可见时会遇到问题:

  MouseArea {
    anchors.fill: parent
    onClicked: inner.visible = !inner.visible
  }

  Rectangle {
    id: outer
    anchors.fill: parent
    color: "blue"
    visible: visibleChildren.length
    Rectangle {
      id: inner
      width: 50
      height: 50      
      color: "red"
    }
  }

Once the parent becomes invisible, the inner item's visibility is always false, even if explicitly set to true. 父项不可见后,即使显式设置为true,内部项目的可见性也始终为false。

What happens is that a child cannot be set to visible if its parent is not visible, so once visibleChildren is empty there is no way to populate it back by setting a child to visible. 发生的情况是,如果子项的父项不可见,则不能将其设置为可见,因此一旦visibleChildren为空,就无法通过将子项设置为可见来填充它。 A child cannot be set to visible while the parent is invisible, and the parent is invisible if there are not visible children. 当父级不可见时,不能将子级设置为可见;如果没有可见的子级,则父级不可见。

So if that kind of behavior is your requirement, you will have to implement some other visibility tracking mechanism instead of using visibleChildren . 因此,如果您需要这种行为,则必须实现其他可见性跟踪机制,而不是使用visibleChildren

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM