Populating Swipe View Dynamically From Model
-
First off, I am very new to QT in general so I apologize for my ignorance but I am not even sure if I am Googling the right things at this point!
I have N number of QML documents that have different views. They all display data provided by a common back end but they all display the data somewhat differently. Some show one data point, others up to 4. It is up to the user to add what types of displays they want and what data providers are hooked up to the data displays. All of the QML Data Display views are based off the same C++ class that inherits QQuickItem.
I have a Stack View that I want to be able to add these "Data Display" views to. The left most page is always the "Main Menu". The right most page is always the "Add Data Display" page and once the user selects all the requisite settings, the Data Display should be added to the Stack View Count - 1 (Right before the "Add Data Display" page).
I have been trying to figure out the best way to do this and I came to the (possible very incorrect) idea of using a QAbstractItemModel to hold a QList of Data Display pointers but I can't seem to figure out how to make it work. I think I can dynamically inflate the QML documents in C++ (Found this in the documentation) and then add them to the model. Then using a repeater, add the model to the StackView but is this really the best way? Are there any examples of something similar floating around?
-
OK. Here is where I am with this:
I have a QAbstractListModel that holds a QList of pointers to QML objects that I load with
QQmlComponent component(&engine, QUrl("qrc:/TestGaugeStyle.qml")); if(component.status() != QQmlComponent::Ready) { qDebug() << "Not Ready: " << component.errorString(); } GaugePresenter *object = (GaugePresenter*) component.create(); QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);
Then I add the created component to the model:
//GaugePresenterManager is a singleton that is designed to manage the //lifecycle of these gauge styles. It also holds the model reference. GaugePresenterManager::instance()->getModel()->addGaugePresenter(object);
When I debug I can see that the model is holding the pointers to the GaugePresenter and everything seems right. This is where I am kind of lost.
I tried creating a property
Q_PROPERTY(GaugePresenterItemModel* gaugePresenterViewModel READ getModel)
and then in QML I assign that property to the model of the SwipeView Repeater.
SwipeView { id: swipeView anchors.fill: parent Repeater{ model: GaugePresenterManager.gaugePresenterViewModel } }
but nothing is displayed. I imagine it is because the property is a pointer to the model and maybe QML doesn't know how to deal with that? Or maybe this just isn't the right way to do this.
The TestGaugeStyle is just a QML document that holds a rectangle with some text that says "test".
I am expecting that when I add the TestGaugeStyle to the Model, the SwipeView will display it.