How to pass 2d Array to qml
-
wrote on 12 Jan 2024, 17:18 last edited by
I want to pass a c++ 2d sudoku empty grid (without numbers in it) to qml. I used a QAbstractTableModel. I do not have errors, only a warning of the unused parameter for the variable role and when I run the project the grid is not displayed in the qml file.
//Grid.h #pragma once #include <QAbstractTableModel> class Grid : public QAbstractTableModel { Q_OBJECT public: explicit Grid(QObject *parent = nullptr); QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role) const override; };
#include "Grid.h" Grid::Grid(QObject *parent) : QAbstractTableModel(parent) {} QModelIndex Grid::index(int row, int column, const QModelIndex &parent) const { Q_UNUSED(parent); return createIndex(row, column); } QModelIndex Grid::parent(const QModelIndex &index) const { Q_UNUSED(index); return QModelIndex(); } int Grid::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return 9; } int Grid::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent); return 9; } QVariant Grid::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); return QVariant(); }
import QtQuick 2.15 import QtQuick.Controls 2.15 import SudokuGrid 1.0 Window { visible: true width: 550 height: 550 title: "Game" GridView { id: grid anchors.fill: parent model: SudokuGrid cellWidth: 50 cellHeight: 50 delegate: Rectangle { width: 50 height: 50 color: model.row % 2 ? "lightgray" : "lightblue" } } }
to pass the grid to qml I used this in main.cpp:
qmlRegisterUncreatableType<Grid>("SudokuGrid", 1, 0, "SudokuGrid", "Cannot display the grid"); -
I want to pass a c++ 2d sudoku empty grid (without numbers in it) to qml. I used a QAbstractTableModel. I do not have errors, only a warning of the unused parameter for the variable role and when I run the project the grid is not displayed in the qml file.
//Grid.h #pragma once #include <QAbstractTableModel> class Grid : public QAbstractTableModel { Q_OBJECT public: explicit Grid(QObject *parent = nullptr); QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role) const override; };
#include "Grid.h" Grid::Grid(QObject *parent) : QAbstractTableModel(parent) {} QModelIndex Grid::index(int row, int column, const QModelIndex &parent) const { Q_UNUSED(parent); return createIndex(row, column); } QModelIndex Grid::parent(const QModelIndex &index) const { Q_UNUSED(index); return QModelIndex(); } int Grid::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return 9; } int Grid::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent); return 9; } QVariant Grid::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); return QVariant(); }
import QtQuick 2.15 import QtQuick.Controls 2.15 import SudokuGrid 1.0 Window { visible: true width: 550 height: 550 title: "Game" GridView { id: grid anchors.fill: parent model: SudokuGrid cellWidth: 50 cellHeight: 50 delegate: Rectangle { width: 50 height: 50 color: model.row % 2 ? "lightgray" : "lightblue" } } }
to pass the grid to qml I used this in main.cpp:
qmlRegisterUncreatableType<Grid>("SudokuGrid", 1, 0, "SudokuGrid", "Cannot display the grid");wrote on 12 Jan 2024, 17:39 last edited by@arlyn123 said in How to pass 2d Array to qml:
qmlRegisterUncreatableType
Your class Grid is empty. What do you want to pass?
-
@arlyn123 said in How to pass 2d Array to qml:
qmlRegisterUncreatableType
Your class Grid is empty. What do you want to pass?
-
wrote on 12 Jan 2024, 18:11 last edited by JoeCFD 1 Dec 2024, 18:13
@arlyn123 I guess you want to display digits as strings. Create a 2D array of QString inside class Grid, assign values to it and override func data of QAbstractTableModel in class Grid. Display text inside delegate with default display role
-
@arlyn123 I guess you want to display digits as strings. Create a 2D array of QString inside class Grid, assign values to it and override func data of QAbstractTableModel in class Grid. Display text inside delegate with default display role
-
@JoeCFD But I want to make a sudoku game with different game levels. Do I need to assign values for each grid?
-
@arlyn123 You have only one GridView in your layout. You can change the values of arrays to update the display on GridView.
-
@JoeCFD also can you please give me an example of how can I work with the data function as you suggested în your first comment
wrote on 12 Jan 2024, 19:01 last edited by -
-
-
1/11