Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

updatePaintNode example causing segmentation fault



  • Hello, I'm just trying to get familiar with updatePaintNode() by running sample code.

    This code runs fine. I see a red rectangle show up
    from this example

    QSGNode *Zones::updatePaintNode(QSGNode * oldNode, UpdatePaintNodeData *)
    {
    QSGSimpleRectNode *n = static_cast<QSGSimpleRectNode *>(oldNode);
        if(!n) {
            n = new QSGSimpleRectNode();
    	n->setColor(Qt::red);
        }
        //n->setRect(boundingRect());
        n->setRect(0,0,500,500);
        n->setColor(Qt::red);
        return n;
    }
    

    But this code causes a segmentation fault.
    from the bezier example

    QSGNode *Zones::updatePaintNode(QSGNode * oldNode, UpdatePaintNodeData *)
    {
        QSGGeometryNode *root = nullptr;
        QSGGeometry *geometry = nullptr;
    
        if(!oldNode){
            root = new QSGGeometryNode;
            geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 3);
            geometry->setDrawingMode(QSGGeometry::DrawLineLoop);
            geometry->setLineWidth(6);
            root->setFlag(QSGNode::OwnsGeometry);
    
            geometry->vertexDataAsPoint2D()[0].set(0, 0);
        	geometry->vertexDataAsPoint2D()[1].set(0,500);
      	geometry->vertexDataAsPoint2D()[2].set(500,500);
    
    
            QSGFlatColorMaterial *material = new QSGFlatColorMaterial;
            material->setColor(Qt::blue);
            root->setMaterial(material);
            root->setFlag(QSGNode::OwnsMaterial);
        }
        else
        {
            root=static_cast<QSGGeometryNode*>(oldNode);
            geometry = root->geometry();
            geometry->allocate(3);
        }
        root->markDirty(QSGNode::DirtyGeometry);
        qDebug("7");
    
       if(!root)
    	qDebug("no root node");
       else
    	qDebug("valid root node");
        return root;
    }
    

    Here is the qml file that it is related

    import QtQuick 2.0
    import DrawZones 1.0
    
    
    Item {
        anchors.fill: parent
    
        Zones {
            id: zoneOB 
            anchors.fill: parent
            objectName: "zone"
        } //Zone
    }
    

  • Qt Champions 2017

    You are missing attach the geometry.

    root->setGeometry(geometry);



  • @dheerendra AH! THANK YOU! @_@