Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Quick elements from C++ - Creating and destroying - Is it possible?

Tags:

c++

qt

qt-quick

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.

like image 521
Michas Avatar asked Nov 30 '25 09:11

Michas


1 Answers

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);
}
like image 179
DRAX Avatar answered Dec 01 '25 22:12

DRAX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!