Important: Please read the Qt Code of Conduct -

Accessing QQuickItem Transform object from C++ ?

  • Hello, I am writing a custom QQuickItem derived class in C++ and registering it in QML to use in my Scene Graph based scene. It's very simple as I'm testing things out yet, here is my QML declaration for my object:

    Hexagon {
            id: hexa0
            anchors.fill: parent
            anchors.margins: 20
            transform: Translate {
                x: 200
                y: 200

    And here is my code for drawing this item:

    QSGNode *Hexagon::updatePaintNode(QSGNode *old_node, UpdatePaintNodeData *) {
    	qDebug() << "updatePaintNode";
    	QSGGeometryNode *node = 0;
    	QSGGeometry *geometry = 0;
    	if (old_node == nullptr) {
    		node = new QSGGeometryNode;
    		geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), _num_points);
    		QSGFlatColorMaterial *material = new QSGFlatColorMaterial;
    		material->setColor(QColor(0, 0, 255));
    	} else {
    		node = static_cast<QSGGeometryNode *>(old_node);
    		geometry = node->geometry();
    	QSGGeometry::Point2D *vertices = geometry->vertexDataAsPoint2D();
    	float angle_offset = 0.0f;
    	float angle_inc = 2*M_PI / _num_points;
    	float angle_rad = qDegreesToRadians(angle_offset);
    	for (int i=0; i<_num_points; i++) {
    		float x = sin(angle_rad) * _radius;
    		float y = cos(angle_rad) * _radius;
    		vertices[i].set(x, y);
    		angle_rad += angle_inc;
    	return node;

    Now, I would like to transform this object from my C++ code. I cannot seem to find how to access the QML declared Transform object in such a way from C++ that I could just do something like transform.setPos(origin.x, origin.y);

    How can I access and set this transform object from the updatePaintNode() method ?
    Thank you!

  • Okay found out the solution, calling setX() and setY() and so also that I removed the anchors from the QML code, now setting translation works! Didn't realize the anchors prevented setX() and setY() from working.

Log in to reply