简体   繁体   English

如何确定 QML 组件当前是否在屏幕上可见

[英]How to determine whether a QML component is currently visible on screen

I have a set of controls which have bindings to frequently changing data values.我有一组控件绑定到频繁更改的数据值。 The data comes from a limited hardware-bus.数据来自有限的硬件总线。 Therefore, it would be better to disable the binding while the control is not visible on screen.因此,最好在控件在屏幕上不可见时禁用绑定。 The item's visible -property doesn't help in this case.在这种情况下,该项目的visible属性没有帮助。 So, how to determine if an Item-based QML widget is currently visible on screen (and not hidden by an overlay or currently outside the visible area)?那么,如何确定基于项目的 QML 小部件当前是否在屏幕上可见(并且未被覆盖层隐藏或当前在可见区域之外)?

Source: https://forum.qt.io/topic/54116/how-to-check-if-a-item-is-currently-visible-on-screen来源: https://forum.qt.io/topic/54116/how-to-check-if-a-item-is-currently-visible-on-screen

I have almost the same problem.我有几乎同样的问题。 Hoping someone here has a solution.希望这里有人有解决方案。

Here's what I would try to get working:这是我要尝试的工作:

First, I presume there is a ScrollView or Flickable in play here?首先,我认为这里有一个 ScrollView 或 Flickable 在玩? If so, then hook to signals like Flickable::movementEnded().如果是这样,则挂钩到 Flickable::movementEnded() 之类的信号。

Second, when that signal fires, use Item::mapToItem() to check if each of your Item's visible rectangle (based on x, y, width, height) intersects your window's contentItem rectangle.其次,当该信号触发时,使用 Item::mapToItem() 检查项目的每个可见矩形(基于 x、y、宽度、高度)是否与窗口的 contentItem 矩形相交。 Set the result as a boolean on each of your items and make sure the data retrieval is disabled when it is false (using && or a tertiary JS expression).在您的每个项目上将结果设置为 boolean 并确保在其为 false 时禁用数据检索(使用 && 或第三 JS 表达式)。

Or if more convenient, remove the binding when false and reapply it with Qt.binding() when true.或者,如果更方便,请在为 false 时删除绑定,并在为 true 时使用 Qt.binding() 重新应用它。

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

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