[Development] Dynamic QML element creation from QML C++ element

Sandro Andrade sandroandrade at kde.org
Mon Nov 21 13:01:21 CET 2011


Hi there,

I'm wondering about the actual reason of getting a
"QDeclarativeContext: Cannot set property on internal context.".
I need to dynamically create new QML elements as child of my custom QML element:

            QDeclarativeRectangle *rect = new QDeclarativeRectangle(this);
            rect->setColor(Qt::red);
            rect->setWidth(30);
            rect->setHeight(30);
            rect->setRadius(2);
            QPoint pos = m_model->data(i, Qt::UserRole+1).toPoint();
            rect->setPos(pos.x(), pos.y());
            qmlContext(this)->setContextProperty("teste", rect);

I've noticed that:
qmlContext(this)->parentContext()->setContextProperty("teste", rect);
actual do the job. Wouldn't that be creating a sibling qml element ?
The just created
element is drawn inside my custom qml element tough, as intended.

TIA,
--
Sandro



More information about the Development mailing list