I would like to use both Qt Quick and Qt Widgets to display the user interface. The problem is I want to create both in C++ code. (The UI isn't defined at time of compilation.) There are no problems with the latter. Unfortunately I cannot find any examples how to create the declarative scene at runtime. It looks it is possible to create a text string with QML code and interpret it. I want something more sane, unfortunately. Is it possible?
I play with QT5, but I am interested in answers about QT4 too.
I believe that this will give you answer: http://www.qtcentre.org/threads/48767-Can-a-QML-element-be-displayed-inside-a-QT-C-GUI
It is possible. Your code should look like this:
// inside custom QGraphicsView class...
...
QGraphicsObject *qmlThing = getQMLGraphicsObjectFromURL(QUrl("pathToYourURL"));
myScene->addItem(qmlThing);
...
QGraphicsObject * MyGraphicsView::getQMLGraphicsObjectFromURL(const QUrl &url) {
QDeclarativeComponent component(new QDeclarativeEngine(), url);
QObject *object = component.create();
return qobject_cast<QGraphicsObject *>(object);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With