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.
In QML I would do onWidthChanged: { my code}. How can I do this on the c++ side?
If you want to respond to size changes in a custom
QQuickItem
, reimplement thegeometryChanged()
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 thewidthChanged()
/heightChanged()
signals.Here's an example of it being reimplemented in Qt Quick Controls 2.
width
isqproperty
that has thewidthChanged
signal, so you can connect that signal to some slot.Similarly, the other properties such as
height
,x
,y
,z
.