Qt 6.11 is out! See what's new in the release
blog
Validate the input in a TableView's rows and column
-
I have a sudoku grid and I want to validate the grid so that a number cannot be entered in the same row and column and if this happens I want the message dialog box to open.
How can I achive that?This is my qml code that has the grid exposed from a c++ QAbstractTableModel and the message dialog.
import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Layouts 1.15 import QtQuick.Controls.Basic 6.0 import Qt.labs.platform import SudokuGrid 1.0 Window { visible: true width: 1920 height: 1080 title: qsTr("Game") color: "#1D2238" MessageDialog { id: msgDial text: "Invalid number." informativeText: "Do you want to save your changes?" buttons: MessageDialog.Ok onAccepted: msgDial.close() } Item { anchors.fill: parent Image { source: "qrc:/UI/assets/bg.png" anchors.fill: parent Button { id: solve x: 406 y: 656 onClicked: grid.solveSudoku() Layout.fillWidth: true background: Rectangle { color: "transparent" } contentItem: Text { color: solve.hovered ? "#606372" : "#ffffff" text: qsTr("Solve") font.family: "Inria Serif" font.pixelSize: 30 scale: 1.4 } } } Rectangle { anchors.centerIn: parent width: 460 height: 460 color: "transparent" Text { anchors.fill: parent font.pixelSize: 32 text: "Sudoku" color: "white" fontSizeMode: Text.Fit minimumPixelSize: 10 wrapMode: Text.WordWrap anchors.rightMargin: 0 anchors.leftMargin: 0 anchors.topMargin: -236 anchors.bottomMargin: 299 horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter } TableView { anchors.fill: parent clip: true model: SudokuGrid { id: grid } delegate: Rectangle { required property var model implicitWidth: 50 implicitHeight: 50 TextField { id: textField anchors.fill: parent validator: IntValidator { bottom: 1; top: 9 } text: model.display !== undefined ? model.display.toString() : "" readOnly: model.display !== undefined && model.display !== 0 horizontalAlignment: TextInput.AlignHCenter verticalAlignment: TextInput.AlignVCenter background: Rectangle { color: model.row % 2 ? "lightpink" : "#bfa6ac" && textField.acceptableInput ? "#FFEFD5" : "#bfa6ac" border { width: 1 color: "white" } } color: "black" } 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" } } } } } }This is the c++ code of the setData function that calls a method from SudokuGenrator.cpp to verify if the number is in the same row or column:
bool Grid::setData(const QModelIndex &index, const QVariant &value, int role) { if (role == Qt::EditRole) { if (!checkIndex(index)) return false; int inputValue = value.toInt(); if (inputValue < 1 || inputValue > 9) return false; SudokuGenerator sudokuGenerator(1); if (sudokuGenerator.verif_complet(index.row(), index.column(), inputValue)) { gridData[index.row()][index.column()] = inputValue; emit dataChanged(index, index); return true; } else { qDebug() << "Invalid number in the same row, column, or 3x3 square!"; } } return false; }And this is the method from SudokuGenerator.cpp:
bool SudokuGenerator::verif_complet(int i, int j, int nr) { if (verif_col(j, nr) == true && verif_lin(i, nr) == true && verif_patrat(i - (i % 3), j - (j % 3), nr) == true) return true; return false; }