[Qt-qml] setData for insert code in qml

michael.brasser at nokia.com michael.brasser at nokia.com
Thu Nov 11 02:59:20 CET 2010


Hi,

On 09/11/2010, at 11:27 PM, ext marvin42 wrote:
> I want insert code in qml.
> 
> i have file main.qml, i try append this code import Qt 4.7\n  Rectangle {  
> width : 1000; height:1000; color: \"blue\" }
> 
> ex:
>     QApplication app(argc, argv);
> 
>       QDeclarativeView view;
> 
>       QDeclarativeContext *context = new  
> QDeclarativeContext(view.rootContext());
> 
>       QDeclarativeComponent component(view.engine());
>       component.setData("import Qt 4.7\n  Rectangle { width : 1000;  
> height:1000; color: \"blue\" }", QUrl("main.qml"));
>       component.create(context);
> 
>       view.setSource(QUrl("main.qml"));
>       view.show();
>      return app.exec();
> 
> But this code don't run.


Can you clarify what you want the above code to do? If you are trying to add an additional Rectangle to the scene described in main.qml, one possible problem is that the Rectangle created is never parented to another item. If you modified the above to something like:

...
QDeclarativeItem *rect = qobject_cast<QDeclarativeItem*>(component.create(context))

view.setSource(QUrl("main.qml"));
rect->setParentItem(view.rootObject())
...

It should be successfully inserted into the scene.

Regards,
Michael



More information about the Qt-qml mailing list