How to pass an array (list) filled with custom data to QML?
-
wrote on 24 Sept 2024, 18:20 last edited by
Good afternoon.
I want to pass a QML list filled with custom class. It seems that this is not so difficult if you make a list of pointers to objects.
They even receive this list in QML, but they cannot understand it.
Custom class#include <QObject> #include <qqml.h> class MinePlace : public QObject { Q_OBJECT QML_ELEMENT public: explicit MinePlace(); int mark = 0; int bomb = 0; int x = 0; int y = 0; };
add to list
void GameCalculate::restartGame() { _forSend.clear(); int label = 0; for(int y = 0;y < _y; ++y) { for(int x = 0;x < _x; ++x) { MinePlace *n = new MinePlace(); n->mark = label; n->bomb = QRandomGenerator64::global()->bounded(0,2); n->x = x; n->y = y; _forSend.append(n); ++label; } } for(auto &a: _forSend) { qDebug() << a->mark; } }
and send
Q_INVOKABLE QList<MinePlace*> getModel(){return _forSend;}
in QML
property var gameModel: null //some code Component.onCompleted: { gameModel = gamecalcID.getModel(); console.info(gamecalcID.getModel()) }
as a result I see the output of the resulting list
qml: [MinePlace(0x1da60e95530),MinePlace(0x1da60e95560),MinePlace(0x1da60e94f60),MinePlace(0x1da60e955f0),MinePlace(0x1da60e95050),MinePlace(0x1da60e95620),MinePlace(0x1da60e95080),MinePlace(0x1da60e950b0),MinePlace(0x1da60e951d0),MinePlace(0x1da60e95650),MinePlace(0x1da60e95140),MinePlace(0x1da60e95170),MinePlace(0x1da60e95200),MinePlace(0x1da60e95590),MinePlace(0x1da60e95230),MinePlace(0x1da60e95260),MinePlace(0x1da60e95290)
It’s clear that these are numbers of memory cells (which is expected, I work with pointers), but how can I get a normal data array from them in QML? And even in an understandable format?
In theory, of course, I can make it simpler - make several properties and store several arrays in them and compare the corresponding values, but this solution seems strange and incorrect to me.
-
@Anton1978 sorry my mistake, I meant :
console.info(gameModel[0].mark) // should be 0 console.info(gameModel[0].mark) // should be 1
wrote on 25 Sept 2024, 22:06 last edited by@Charby said in How to pass an array (list) filled with custom data to QML?:
console.info(gameModel[0].mark) // should be 0 console.info(gameModel[0].mark) // should be 1
console.info(gameModel[
1
].mark) // should be 1
:)
As @Charby wrote above, you can access the list data using the index and the member you want to address, like this:
(with the changes shown below)// return a list reference // consider to make it const if you dont plan to modify the list later (only in C++ code) Q_INVOKABLE const QList<MinePlace *> &getModel() { return _forSend;}
In QML something like
property var gameModel = gamecalcID.getModel(); for (var mp in gameModel) console.log(mp.mark)
-
wrote on 25 Sept 2024, 01:41 last edited by
Out of curiosity, what is the ouput of console.info(gameModel[0].label) ?
-
Out of curiosity, what is the ouput of console.info(gameModel[0].label) ?
-
wrote on 25 Sept 2024, 21:25 last edited by
@Anton1978 sorry my mistake, I meant :
console.info(gameModel[0].mark) // should be 0 console.info(gameModel[0].mark) // should be 1
-
wrote on 25 Sept 2024, 21:27 last edited by
it seems to me that you successfully passed an array (list) filled with custom data to QML...or do you need something else ?
-
@Anton1978 sorry my mistake, I meant :
console.info(gameModel[0].mark) // should be 0 console.info(gameModel[0].mark) // should be 1
wrote on 25 Sept 2024, 22:06 last edited by@Charby said in How to pass an array (list) filled with custom data to QML?:
console.info(gameModel[0].mark) // should be 0 console.info(gameModel[0].mark) // should be 1
console.info(gameModel[
1
].mark) // should be 1
:)
As @Charby wrote above, you can access the list data using the index and the member you want to address, like this:
(with the changes shown below)// return a list reference // consider to make it const if you dont plan to modify the list later (only in C++ code) Q_INVOKABLE const QList<MinePlace *> &getModel() { return _forSend;}
In QML something like
property var gameModel = gamecalcID.getModel(); for (var mp in gameModel) console.log(mp.mark)
-
1/6