As you know, a QQuickFramebufferObject::Renderer subclass shouldn't access its parent item's properties directly, and instead should copy them to itself in its synchronize()
method. So the code tends to look like this (using the AUTO_PROPERTY
macro from here):
class MyItem : public QQuickFramebufferObject {
AUTO_PROPERTY(int, foo)
AUTO_PROPERTY(float, bar)
// ...
};
class MyItemRenderer : public QQuickFramebufferObject::Renderer {
public:
void synchronize(QQuickFrameBufferObject* qqfbo) {
auto src = (MyItem*)qqfbo;
foo = src->foo();
bar = src->bar();
}
private:
int foo;
float bar;
// ...
};
I wanted to avoid duplicating the declaration of properties between the two classes, so right now I'm implementing this alternative solution:
class MyItem : public QQuickFramebufferObject {
AUTO_PROPERTY(int, foo)
AUTO_PROPERTY(float, bar)
// ...
};
class MyItemRenderer : public QQuickFramebufferObject::Renderer {
public:
MyItemRenderer() {
copiedData = new MyItem();
}
void synchronize(QQuickFrameBufferObject* qqfbo) {
auto src = (MyItem*)qqfbo;
copiedData.setFoo(src->foo());
copiedData.setBar(src->bar());
}
private:
MyItem* copiedData;
};
I still need to write out and maintain the copying code as you see, but it's better than the other way.
Are there gotchas with doing that? (creating an instance of a QQuickItem subclass which instance I don't intend to render or add to a QML tree).
Quote from the docs:
This is useful for creating QML objects from C++ code, whether to display a QML object that can be visually rendered, or to integrate non-visual QML object data into a C++ application.
This seems to imply that such nonvisual usage is an intended and supported use case. But I'm not sure I'm reading it right.
QtObject
for non-visual stuff, less overhead. The limitation is it can only have properties, not children, which is kinda silly, considering that it is essentially aQObject
which can have children, the functionality is just not exposed to QML. – dtech