Pass 2d array c++ to qml using GridView
Unsolved
QML and Qt Quick
-
I have a sudoku matrix in c++ code:
#include "SudokuModel.h" SudokuModel::SudokuModel(QObject *parent) : QObject(parent) { sudokuMatrix = { {5, 3, 0, 0, 7, 0, 0, 0, 0}, {6, 0, 0, 1, 9, 5, 0, 0, 0}, {0, 9, 8, 0, 0, 0, 0, 6, 0}, {8, 0, 0, 0, 6, 0, 0, 0, 3}, {4, 0, 0, 8, 0, 3, 0, 0, 1}, {7, 0, 0, 0, 2, 0, 0, 0, 6}, {0, 6, 0, 0, 0, 0, 2, 8, 0}, {0, 0, 0, 4, 1, 9, 0, 0, 5}, {0, 0, 0, 0, 8, 0, 0, 7, 9} }; } int SudokuModel::getValue(int row, int col) const { return sudokuMatrix[row][col]; }
And its header:
#ifndef SUDOKUMODEL_H #define SUDOKUMODEL_H #include <QObject> #include <QList> class SudokuModel : public QObject { Q_OBJECT public: explicit SudokuModel(QObject *parent = nullptr); Q_INVOKABLE int getValue(int row, int col) const; private: QList<QList<int>> sudokuMatrix; }; #endif
And I want to pass its values using setContextProperty in a qml GridView. How can I do that?
I also read that you can also pass a c++ 2d array to qml using QAbstractTableModel but how can I work with it? Does anyone has an example with a QAbstractTableModel? -
@arlyn123 said in Pass 2d array c++ to qml using GridView:
qVariantFromValue
It is obsolete, see https://doc.qt.io/qt-5/qvariant-obsolete.html#qVariantFromValue