I made this class to use it as data container. I read from a json some data (in c++) and fill a list of GUIArea that I store in the m_guiAreas list in the dataHandler. At a certain point from QML I request a series of selectedAreas to the dataHandler. DataHandler fill the QList m_selectedGuiAreas and emit the selectedAreasChanged() signal. Now I expect to se a grid of rectangle filled with the selected Datas but I don't see anything. At C++ level, when selectedAreasChanged() is emitted, the m_selectedGuiAreas result filled with right data but at QML level it seems empty or maybe that datas aren't read right way.
Here is thw class I use as wrapper to bring datas to QML level:
class GUIArea : public QObject
{
Q_OBJECT
Q_PROPERTY(QString id READ id )
Q_PROPERTY(QString configurations READ configurations )
...
public:
explicit GUIArea(QObject *parent = nullptr): QObject (parent) {}
QString id() {return m_id;}
void id(QString newId) {m_id = newId;}
QString configurations() {return m_configuration; }
void configurations(QString newConfiguration) {m_configuration = newConfiguration;}
...
private:
QString m_id;
QString m_configuration;
};
here below is the dataHandler class where I declare the lists of data read from Json and that I convert from Qlist to QQmlPropertyList (I see this in some QML guide for exposing c++ properties to QML). The method initializeDatas read the datas storing them in the m_GUIAreas and then select ones to send to QML in m_selectedGUIAreas, emitting in the end the signal selectedGUIAsChanged().
class dataHandler : public QObject
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<GUIArea> selectedGuiAreas READ selectedGuiAreas NOTIFY selectedAreasChanged)
public:
explicit dataHandler(QObject *parent = nullptr);
static dataHandler* instance();
QQmlListProperty<GUIArea> selectedGuiAreas();
...
public slots:
void initializeDatas(const json& blocksList);
...
signals:
...
void selectedAreasChanged();
...
private:
...
QList<GUIArea *> m_guiAreas;
QList<GUIArea *> m_selectedGuiAreas;
};
in the main file then the dataHandler is declared as a property: here is the code:
QQuickView view;
...
view.engine()->rootContext()->setContextProperty("dataHandler", dataHandler::instance());
...
view.show();
a part of the page I want to visualize in QML is here below. AreaButton is a Rectangle within a Text and a alias of the property text.
Grid {
id: areasButtonsGrid
columns: 4
anchors.fill: parent
Repeater {
model: dataHandler.selectedGuiAreas
delegate:
AreaButton {
text: qsTr(model.modelData.programName)
}
}
}
dataHandler
as context property? See doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html – Amfasis