The right way to dynamically create qml windows

I am working on a Qt application and I need to dynamically create windows. Each window consists of a QObject back back based and qml interface. Each window should be connected to a set of signals emitted by the base classes. The current solution is to output the window from QQuickView, connect signals to it, and load qml with setSource(). Is this the right way or is there a better way? Is it better to use one QQmlEnginefor all windows (and use this engine as the parent for each window) or create a new engine for each new window?

+4
source share
1 answer

For this, I would provide a C ++ model in QML code. Since this model will be dynamic (elements can be added or removed), I would use a derived model QAbstractItemModelthat can inform the view that some elements are added / removed. Using something else like this QList<QObject*>would mean that you would need to say that the whole model needs to be reloaded after every change.

Instead of implementing the model from scratch, you can use a class, for example QQmlObjectListModelfrom Qt QML Tricks , it provides QList- as an API from C ++, but QAbstractItemModelit displays properties QObjectas roles under the scene.

, , QObjects, benlau QSyncable ( , Window, ).

QQmlApplicationEngine setContextProperty. A QQuickView - , , , QML-.

QML- Instantiator , Window :

Instantiator {
    model: yourModel
    Window {
        /* ... */
    }
}
+1

Source: https://habr.com/ru/post/1673907/


All Articles