3

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.

Michas
  • 8,534
  • 6
  • 38
  • 62
  • It is possible check this answer out: http://stackoverflow.com/questions/16002310/qml-how-to-draw-multiple-rectangulars-in-random-places/16004056#16004056 - there are a few different ways to create QML objects dynamically, but it is all from QML. Creating QML from C++ would be considered counter-productive, because it negates the separation of logic (C++) and UI (QML). – dtech Aug 19 '13 at 14:26
  • The problem is the UI need to be generated at runtime. – Michas Aug 20 '13 at 15:30
  • With QML the UI is ALWAYS generated at runtime, QML is just markup that gets passed, the appropriate constructors are called and the appropriate properties are set. You can generate strings of QML and "execute it" as the answer I linked to shows. – dtech Aug 20 '13 at 15:34
  • Well. It would be horrible separation to have logic in both C++ and QML. – Michas Aug 20 '13 at 15:49
  • There is no problem in having logic in QML as long as it is not something that requires high performance. The problem would be if you have UI in the C++ part - that is why it needs to be fully isolated and interfaced through signals/slots and properties. This way the same logic can work with both QML and QtWidgets. – dtech Aug 20 '13 at 15:56
  • I don't see why writing the whole UI in C++ is a bad idea. – Michas Aug 20 '13 at 21:35
  • The bad idea is to have QML UI in the C++ logic. C++ should not know about QML. There is nothing wrong with implementing the UI with QWidgets and C++. – dtech Aug 20 '13 at 21:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35867/discussion-between-michas-and-ddriver) – Michas Aug 20 '13 at 21:42

2 Answers2

1

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);
}
DRAX
  • 30,559
  • 2
  • 26
  • 28
0

It would seem that generating QML code at run time is the way to go. Depending on how dynamic the UI really has to be, you could go with a text templating library, see e.g. C++ HTML template framework, templatizing library, HTML generator library for a list of these. In this way you have a skeleton for your UI in a file, and you just fill out the changing parts with a call to the templating library of your choice, similarly to how dynamic web pages are generated.

Community
  • 1
  • 1
Daniel Landau
  • 2,264
  • 2
  • 15
  • 19