Implementing updatePaintNode causes other QML elements to also draw incorrectly

  • I am trying to learn how to do basic drawing with a subclassed QQuickItem. Using this simple code to draw a line, my QML element does in fact draw a line, but having an implementation of this function also causes other, unrelated QML elements (which are not subclassed in C++) to draw incorrectly by moving their positions, layout and clipping. Apparently the Scene Graph is getting messed up by just having one single element with custom drawing in it:

    @ QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data){

            QSGGeometry *geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 2);
            geometry->vertexDataAsPoint2D()[0].set(0, 0);
            geometry->vertexDataAsPoint2D()[1].set(width(), height());
            QSGFlatColorMaterial *material = new QSGFlatColorMaterial;
            material->setColor(QColor(255, 0, 0));
            QSGGeometryNode *node = new QSGGeometryNode;
            delete oldNode;
            return node;

    I'm guessing the cause of this problem lies outside this function. If I simply comment out this line in the class's constructor, the above function of course isn't called and everything returns to normal:

    @setFlag(QQuickItem::ItemHasContents, true);@

    What could cause the Qt Quick SceneGraph to get all distorted when trying to do this?

  • It looks like the drawing code itself is responsible for messing up other QML elements, which surprises me. If my implementation of this function only says return oldNode; and nothing else, the rest of my QML scene looks fine. I am following a very simple official example for drawing a line and this code is copied over from that. I can't see anything that should cause this behavior.

  • I have discovered that @geometry->setLineWidth(3); @in the code above extends to other QML elements and can distort them, even if those other QML elements are "normal" QML elements (with no QQuickItem subclass beneath them). This seems odd to me that you could affect other elements and I wonder if it is a bug? The documentation says that this function should only affect the current element, but that is not my experience. Can anyone weigh in on why @geometry->setLineWidth(3); @has such unwieldy power?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.