setData function from qml to edit cells
-
I've made a sudoku grid using a QAbstractTableModel c++ class. I want to edit the cells that are empty.
Here if I enter something and then press enter nothing happend in the UI. The value is not displayed.
I think the problem can be the setData function but I don't know how to make it work because I don't know how to use it.
How can I make the numbers to be displayed in the grid?#include "Grid.h" Grid::Grid(QObject *parent) : QAbstractTableModel(parent) { int initialValues[9][9] = { {7, 0, 2, 0, 5, 0, 6, 0, 0}, {0, 0, 0, 0, 0, 3, 0, 0, 0}, {1, 0, 0, 0, 0, 9, 5, 0, 0}, {8, 0, 0, 0, 0, 0, 0, 9, 0}, {0, 4, 3, 0, 0, 0, 7, 5, 0}, {0, 9, 0, 0, 0, 0, 0, 0, 8}, {0, 0, 9, 7, 0, 0, 0, 0, 5}, {0, 0, 0, 2, 0, 0, 0, 0, 0}, {0, 0, 7, 0, 4, 0, 2, 0, 3} }; for (int row = 0; row < 9; ++row) { for (int col = 0; col < 9; ++col) { gridData[row][col] = initialValues[row][col]; } } } int Grid::rowCount(const QModelIndex &) const { return 9; } int Grid::columnCount(const QModelIndex &) const { return 9; } QVariant Grid::data(const QModelIndex &index, int role) const { int row = index.row(); int col = index.column(); switch (role) { case Qt::DisplayRole: return gridData[row][col]; } return QVariant(); } bool Grid::setData(const QModelIndex &index, const QVariant &value, int role) { if (role == Qt::EditRole) { if (!checkIndex(index)) return false; gridData[index.row()][index.column()] = value.toInt(); return true; } return false; } Qt::ItemFlags Grid::flags(const QModelIndex &index) const { return Qt::ItemIsEditable | QAbstractTableModel::flags(index); }
TableView { id: tableView anchors.fill: parent clip: true model: SudokuGrid {} delegate: Rectangle { implicitWidth: 50 implicitHeight: 50 border { color: "white" width: 1 } Rectangle { width: 1 height: parent.height color: model.column % 3 == 0 ? "black" : "transparent" } Rectangle { width: parent.width height: 1 color: model.row % 3 == 0 ? "black" : "transparent" } color: model.row % 2 ? "lightpink" : "lightblue" Text { anchors.centerIn: parent text: display font.pointSize: 12 } TableView.editDelegate: TextField { anchors.fill: parent text: display horizontalAlignment: TextInput.AlignHCenter verticalAlignment: TextInput.AlignVCenter Component.onCompleted: selectAll() onAccepted: { tableView.commit() } TableView.onCommit: { display = text } } }
-
@arlyn123 said in setData function from qml to edit cells:
bool Grid::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::EditRole) {
if (!checkIndex(index))
return false;gridData[index.row()][index.column()] = value.toInt(); return true; } return false;
}
Can you try? But you need to make sure setData func is called at first.
emit dataChanged( index ); may not be needed.
https://doc.qt.io/qt-6/qabstractitemmodel.html#dataChangedbool Grid::setData(const QModelIndex &index, const QVariant &value, int role) { if (role == Qt::EditRole) { if (!checkIndex(index)) return false; gridData[index.row()][index.column()] = value.toInt(); emit dataChanged( index ); return true; } return false; }
-
@arlyn123
As @JoeCFD has said. If you change a value in a model you mustemit dataChanged(...)
. That is what the table view --- widget or QML --- listens for to know it needs to re-read the value and update. If you do not, it does not refresh of its own accord.If you write your own
setData()
override then either it must do the emit or it must call an inheritedsetData()
which will do that.