July 13, 2012

qtNiks qtNiks
Lab Rat
4 posts

creating QML object dynamically

 

Is there anyway for creating a QML object dynamically from the Qt code, without using the .qml file or JavaScripts?

5 replies

July 13, 2012

AlterX AlterX
Ant Farmer
123 posts

yes it is!

  1. QDeclarativeItem                *item = NULL;
  2. QDeclarativeComponent       component(this->rootContext->engine(), QUrl("qrc:/my.qml"));
  3.  
  4. item = qobject_cast<QDeclarativeItem *>(component.create());
  5. item->setProperty("property", "Hello");
  6. item->setParentItem(qobject_cast<QDeclarativeItem *>(parent));

You can safely pass a QML object (parent in the example) from within a QML file as parameter

July 13, 2012

Skylar Skylar
Ant Farmer
47 posts

There is a possibility to build QQmlExtensionPlugin to define your qmlfiles and refenrece them.

Is it the pint you think about ?

http://doc-snapshot.qt-project.org/5.0/qqmlextensionplugin.html

There is the an example in the TimeExample in the QT5 sources.

 Signature 

In most cases, the bug is mine. But of the remaining lines are correct.

July 19, 2012

qtNiks qtNiks
Lab Rat
4 posts

I already have a qml file and i want to add this declarative item on that QML file and be able to change the property for that declarative item. How to do that?
Below is the code snippet

int main(int argc, char *argv[])
{

QApplication app(argc, argv); QmlApplicationViewer viewer; viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto); viewer.setMainQmlFile(QLatin1String(“qml/example_2/main.qml”)); QObject *rootObjectA = viewer.rootObject(); rootObjectA->setProperty(“text1Text”,QVariant(“Change you text here…”)); viewer.showExpanded(); return app.exec(); }

July 19, 2012

AlterX AlterX
Ant Farmer
123 posts

qtNiks wrote:
I already have a qml file and i want to add this declarative item on that QML file and be able to change the property for that declarative item. How to do that?
Below is the code snippet

int main(int argc, char *argv[])
{

QApplication app(argc, argv); QmlApplicationViewer viewer; viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto); viewer.setMainQmlFile(QLatin1String(“qml/example_2/main.qml”)); QObject *rootObjectA = viewer.rootObject(); rootObjectA->setProperty(“text1Text”,QVariant(“Change you text here…”)); viewer.showExpanded(); return app.exec(); }

Maybe I didn’t understand what you want to do…if you just want to change a property on rootObjectA, the code above is correct…

July 20, 2012

MartinJ MartinJ
Lab Rat
14 posts

I’m not sure I understand exactly what you’re looking for either. Perhaps you want to create an object from QML defined entirely in C++. QQmlComponent::setData() does this, e.g.

  1. QDeclarativeComponent component(engine);
  2. component.setData("import QtQuick 1.0; Rectangle { color: 'red'; width: 20; height: 20 }", QUrl());
  3. QObject *obj = component.create();

 
  ‹‹ [SOLVED] Size of layout elements      [SOLVED] Error launching my App after PR 1.3 ››

You must log in to post a reply. Not a member yet? Register here!