Different sudoku game levels in the same qml

70 views Asked by At

How can I make a Sudoku game in c++ with 5 diffrent game levels but in the same qml file? What is the easiest way to do it? How can I modify my code in this scope?

I've made a sudoku grid in c++ and I pass it to qml using QAbstractTableModel and TableView.

This is the TableView:

TableView {
                anchors.fill: parent
                columnSpacing: 1
                rowSpacing: 1
                clip: true

                model: SudokuGrid {}

                delegate: Rectangle {
                    implicitWidth: 50
                    implicitHeight: 50
                    border {
                         color: "white"
                         width: 1
                    }
                    color: model.row % 2 ? "lightpink" : "lightblue"
                    Text {
                        anchors.centerIn: parent
                        text: display
                        font.pointSize: 12
                    }
                }
            }

This is the c++ code:

    #include "Grid.h"

Grid::Grid(QObject *parent) : QAbstractTableModel(parent)
{
    for (int row = 0; row < 9; ++row) {
        for (int col = 0; col < 9; ++col) {
            gridData[row][col] = (row * 3 + row / 3 + col) % 9 + 1;
        }
    }
}

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
{
    switch (role) {
    case Qt::DisplayRole:
        return gridData[index.row()][index.column()];
    default:
        break;
    }

    return QVariant();
}

QHash<int, QByteArray> Grid::roleNames() const
{
    return { {Qt::DisplayRole, "display"} };
}

And it's header:

    #pragma once

#include <QAbstractTableModel>

class Grid : public QAbstractTableModel
{
    Q_OBJECT

public:
    explicit Grid(QObject *parent = nullptr);

public:
    int rowCount(const QModelIndex & = QModelIndex()) const override;

    int columnCount(const QModelIndex & = QModelIndex()) const override;

    QVariant data(const QModelIndex &index, int role) const override;

    QHash<int, QByteArray> roleNames() const override;

signals:

private:
    int gridData[9][9];
};


       
0

There are 0 answers