C++ QQuickItem:如何在项目大小更改时触发函数 [英] C++ QQuickItem: How to trigger a function on item size change
问题描述
我在 C++ 中有一个 QQuickItem,它在 QML 应用程序中实例化.如果我的 QQuickItem 的 width 属性发生变化,我想在 C++ 端触发一个自定义函数.
I have a QQuickItem in C++ which is instantiated in a QML application. I want to trigger a custom function on the c++ side if the width property of my QQuickItem changes.
在 QML 中,我会做 onWidthChanged: { my code}.我怎样才能在 C++ 方面做到这一点?
In QML I would do onWidthChanged: { my code}. How can I do this on the c++ side?
推荐答案
如果您想响应自定义 QQuickItem
中的大小更改,请重新实现 geometryChanged()
函数.这样做的好处是不会为每个创建额外的信号槽连接项,如果您要连接到 widthChanged()
/heightChanged()
信号,这将是必需的.
If you want to respond to size changes in a custom QQuickItem
, reimplement the geometryChanged()
function. This has the benefit of not creating extra signal-slot connections for each item, as would be necessary if you were to connect to the widthChanged()
/heightChanged()
signals.
这是在 Qt Quick Controls 2 中重新实现的示例.
Here's an example of it being reimplemented in Qt Quick Controls 2.
这篇关于C++ QQuickItem:如何在项目大小更改时触发函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!