Question

I know, it's possible to define a QObject with custom properties and expose this object in QML environment. But this way, for each new property I'd need to recompile C++ code.

Is it possible, to make a dynamic binding from C++/Qt to QML-objects? Something like:

//C++ code: 
updateProperty("myQmlObject.any_property", "Hello World");

Thank you!

SOLVED:

_view->rootContext()->setContextProperty( "cppmessage" , "Hello from C++" );

WHERE: view is a QDeclarativeView, and cppmessage is used in QML without prior declaration like: "text: cppmessage"

This link was usefull for finding the solution: http://xizhizhu.blogspot.com/2010/10/hybrid-application-using-qml-and-qt-c.html

Was it helpful?

Solution

Yes, This can be done. Link

// MyItem.qml
import QtQuick 1.0

Item {
    property int someNumber: 100
}

//C++
QDeclarativeEngine engine;
QDeclarativeComponent component(&engine, "MyItem.qml");
QObject *object = component.create();

qDebug() << "Property value:" << QDeclarativeProperty::read(object,"someNumber").toInt();
QDeclarativeProperty::write(object, "someNumber", 5000);

qDebug() << "Property value:" << object->property("someNumber").toInt();
object->setProperty("someNumber", 100);

Edit:1 Another way to do it , as suggested by @Valentin is listed here link

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top