简体   繁体   中英

Sudoku GUI using QT5 with C++

The basic functionality should be to read a sudoku puzzle from a text file and arrange the values of the file in a 9x9 grid of QPushButtons with only the ones labelled "X" being enabled. I used an array to create the buttons and connected them all to a single function that should increment the value and thus the label displayed from 1-9 and then pass that value back into the initial grid. However, I've noticed that the way I do it, it changes all the values where grid[i][j]=0 to the number incremented. How would I go about fixing that?

//window.h
#include <QWidget>
#include <QMouseEvent>
#include <QPushButton>
#include <QFileDialog>
#include<iostream>
#include <vector>
#include <map>

using namespace std;

class Window : public QWidget
{
    Q_OBJECT

public:
    Window(QWidget *parent = 0);
    int value=0;
    int grid[9][9];
    int flag[9][9];
    void paintEvent(QPaintEvent *event);
    void mouseReleaseEvent(QMouseEvent * event);

protected:
   // methods and membersss

private slots:
    // event handlers (in Qt "slots")
    // ...
    void handleButton();
  private:
    QPushButton*btn[9][9];
};
//window.cpp
#include "window.h"

#include <QPainter>
#include <QDebug>
#include <QTextStream>
#include <QPushButton>
#include <QFileDialog>
#include<QFont>
#include<QColor>

#include <map>
#include <vector>

using namespace std;

// Main window class
/*Window::Window(QWidget *parent) : QWidget(parent)
{
   // set form size
   setFixedSize(400,400);
   setWindowTitle("Bla....");

   // create widgets....

}*/



Window::Window(QWidget *parent) : QWidget(parent)
{
   // set form size
   setFixedSize(1000,1000);
   setWindowTitle("Sudoku Board");
   QFile file("sudoku.txt");
   if (!file.open(QIODevice::ReadOnly|QIODevice::Text))
       puts("Error");

   while(!file.atEnd())
   {
   for(int i=0;i<9;i++)
   {
       QString line=file.readLine();
       for (int j=0;j<9;j++)
       {
           if(line[j]=='X')
           {

               grid[i][j]=0;
               flag[i][j]=1;
           }
           else
           {
               grid[i][j]=line[j].digitValue();
               flag[i][j]=0;
           }
       }
   }
}



    QPushButton *btn[9][9];

  for (int i=0; i<9; i++) {
   for (int j=0; j<9; j++) {
    if (flag[i][j]==1)
    {
    btn[i][j] = new QPushButton("X", this);
    btn[i][j]->setGeometry(50*(2*j+1),50*(2*i+1),1000,1000);
    btn[i][j]->setFixedSize(90, 90);
    btn[i][j]->setStyleSheet(" color: blue" );

     connect(btn[i][j], &QPushButton::released, this, &Window::handleButton);
    }
    else
    {
        btn[i][j] = new QPushButton(QString::number(grid[i][j]), this);
        btn[i][j]->setGeometry(50*(2*j+1),50*(2*i+1),1000,1000);
        btn[i][j]->setFixedSize(90, 90);
        btn[i][j]->setEnabled(false);
        btn[i][j]->setStyleSheet(":disabled { color: black}" );
    }

   }

  }


}

void Window::handleButton()
{

    QPushButton*button[9][9];
    for(int i=0;i<9;i++)
    {
        for (int j=0;j<9;j++)
        {
    button[i][j]=qobject_cast<QPushButton *>(sender());







    if (grid[i][j]==9)
    {
        grid[i][j]=1;
        button[i][j]->setText(QString::number(grid[i][j]));
    }
    else
    {
    grid[i][j]++;
    button[i][j]->setText(QString::number(grid[i][j]));
    }
    }
}
}


void Window::paintEvent(QPaintEvent * event)
{


    QPainter painter(this);
    QPen pen;
    QFont font;
    font.setPixelSize(30);
    pen.setWidth(4);
    painter.setPen(pen);
    painter.setFont(font);

    for(int i=0;i<3;i++)
    {
        for (int j=0;j<3;j++)
        {
            painter.drawLine(45,45,45,950);
            painter.drawLine((300*j)+45,45,(300*j)+45,950);
            painter.drawLine(950,45,950,950);
            painter.drawLine(45,45,950,45);
            painter.drawLine(45,(300*j)+45,950,(300*j)+45);
            painter.drawLine(45,950,950,950);

        }
    }
 }

void Window::mouseReleaseEvent(QMouseEvent * event)
{
    // get click position
    //qDebug() << "Mouse x " << event->x() << " Mouse y " << event->y();
    int xcoords=event->pos().x();
    int ycoords=event->pos().y();
    cout<<xcoords<<" "<<ycoords<<endl;

    for(int i=0;i<9;i++)
    {
        for (int j=0;j<9;j++)
        {

            cout<<" "<<grid[i][j]<<" ";
        }
    }
    update();
}

I'm pretty new to this so any advice is appreciated. what i want it to look like roughly

ive solved the problem by implementing a second signal to take care of indexing the buttons

#include "widget.h"

Widget::~Widget()
{
}

Widget::Widget(QWidget *parent) : QWidget(parent)
{
   // set form size
   setFixedSize(1000,1000);
   setWindowTitle("Sudoku Board");
   QFile file("/elec1204/P1/sudoku.txt");
   if (!file.open(QIODevice::ReadOnly|QIODevice::Text))
       puts("Error");

   while(!file.atEnd())
   {
   for(int i=0;i<9;i++)
   {
       QString line=file.readLine();
       for (int j=0;j<9;j++)
       {
           if(line[j]=='X')
           {

               grid[i][j]=0;

           }
           else
           {
               grid[i][j]=line[j].digitValue();

           }
       }
   }
}





  for (int i=0; i<9; i++) {
   for (int j=0; j<9; j++) {
    if (grid[i][j]==0)
    {
    btn[i][j] = new QPushButton("X", this);
    btn[i][j]->setGeometry(50*(2*j+1),50*(2*i+1),1000,1000);
    btn[i][j]->setFixedSize(100, 100);

     connect(btn[i][j], SIGNAL(clicked()), this, SLOT(Signalcreator()));
    }
    else
    {
       btn[i][j] = new QPushButton(QString::number(grid[i][j]), this);
        btn[i][j]->setGeometry(50*(2*j+1),50*(2*i+1),900,900);
        btn[i][j]->setFixedSize(100, 100);
    }

   }

  }
       connect(this, SIGNAL(handleButtonSignals(int,int)), this, SLOT(handleButton(int,int)));


}

void Widget::Signalcreator() {
    int i,j;
    for (int a=0; a<9; a++) {
        for (int b=0; b<9; b++) {
            if (qobject_cast<QPushButton*>(sender()) == btn[a][b]) {
                i = a;
                j = b;
            }
        }
    }
    qDebug() << "The button: x = " << i << " and y = " << j;
    emit handleButtonSignals(i, j);
}

void Widget::handleButton(int i, int j)
{



    if (grid[i][j]==9)
    {
        grid[i][j]=1;
        btn[i][j]->setText(QString::number( grid[i][j]));

    }
    else
    {
        grid[i][j]++;
        btn[i][j]->setText(QString::number(  grid[i][j]));

    }
    update();
}


void Widget::mouseReleaseEvent(QMouseEvent * event)
{
    // get click position
    qDebug() << "Mouse x " << event->x() << " Mouse y " << event->y();
    int xcoords=event->pos().x();
    int ycoords=event->pos().y();
    cout<<xcoords<<" "<<ycoords<<endl;

    for(int i=0;i<9;i++)
    {
        for (int j=0;j<9;j++)
        {

            cout<<" "<<grid[i][j]<<" ";
        }
    }
    update();
}

It works fine now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM