Magnus2552
Magnus2552

Reputation: 77

Qt QML draw in cpp subclass

so I am trying to understand drawing in Qt, but I dont get this one: So first of all I had a qml file where I embedded a custom class called Game which has this constructor: Game::Game(QQuickItem *parent) : QQuickItem(parent) Writing setFlag(ItemHasContents, true); in the constructor code was sufficient to being able to draw with QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *); using QSGGeometry (This worked). Now I tried to draw in a subclass, using the same procedure but now it does not work anymore: So what I did was create a class called qcurver, and in Game I create an instance of qcurver. QCurver has the same constructor like Game and I pass the parent of Game so that technically it has the same parent as Game, but now when I try to draw in this class, nothing happens. Does anyone know, how I can pass the QML object for drawing, so that I actually see the drawing?

Upvotes: 2

Views: 617

Answers (1)

Velkan
Velkan

Reputation: 7582

You're creating a Game object from QML code, so it's registered correctly in the QML scene. But the QCurver isn't created there, so it won't work.

Yes, it has the same parent, but it only means that if the parent gets deleted - the QCurver will be deleted too. Qt doesn't revise all QObjects that are added as children to find out if they are of type QQuickItem and should be rendered.

Also, QML itself was added to be able to avoid doing widget hierarchies in C++. So, compose hierarchies in QML. Or, if you want some of them in C++ for the performance reasons - then it is possible to do QSGNode hierarchies inside a QQuickItem (that's useful for rendering complex scenes with OpenGL).

Upvotes: 2

Related Questions