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->setDrawingMode(GL_LINES);
geometry->setLineWidth(3);
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;
node->setGeometry(geometry);
node->setFlag(QSGNode::OwnsGeometry);
node->setMaterial(material);
node->setFlag(QSGNode::OwnsMaterial);
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?
↧