R
R
Robotex2015-08-17 18:34:01
Qt
Robotex, 2015-08-17 18:34:01

How to create a QML object displaying the properties of a C++ object?

Those. Let's say we have a spaceship object in the game. All manipulations with it (including creation and deletion) are performed on the C++ side. QML is only required to draw it on the stage according to the current properties. How can this be implemented? The main problem is to create a QML object at the same time as a C++ object and at the same time ensure that the properties of the correct C++ object are displayed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Jacob E, 2015-08-18
@Robotex

It is done quite simply:

  • create a ship class, all the fields of which we pass through Q_PROPERTY
  • register the ship type in the engine
  • we create a wrapping class that will give a list of ships through QQmlListProperty and QList (or just throw our model inherited from QAbstractListModel into the engine)
  • we get this list in QML and draw as we want

M
Mak Alexey, 2015-08-17
@S-ed

Qt allows you to create QML from a string:

var newObject = Qt.createQmlObject(
'import QtQuick 1.0;Rectangle {color: "red"; width: 20; height: 20}',
    parentItem, "dynamicSnippet1");
doc.qt.io/qt-4.8/qdeclarativedynamicobjects.html
Then just add/change the required fields:
doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfro...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question