Access properties of QML objects from C ++ definition withoun Q_PROPERTY

I know that you can define a QObject with custom properties and expose this object in a QML environment. But in this way, for each new property, I will need to recompile the C ++ code.

Is it possible to make dynamic binding with C ++ / Qt to QML objects? Sort of:

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

Thank!

SOLVE:

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

WHERE: view is a QDeclarativeView, and cppmessage is used in QML without first declaring type: "text: cppmessage"

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

+3
source share
1 answer

Yes, it 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);

: 1 , @Valentin,

+2

All Articles