开发者

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

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: cppm开发者_如何学运维essage"

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜