WanderingSoul
18th March 2015, 20:49
Hello there,
I've been working on a GUI for a simple puzzle game where the program generates
numbers for a 9x9 grid. I've represented that grid as 81 different, stylized QPushButtons
and everything works so far. When the numbers are generated, each appear in its
allocated slot. The problem occurs when the user clicks the "New Game" Menu action
button (which is supposed to "reset" the grid and generate new random numbers); the
QPushButtons retain their text and do not "reset".
Here's the code I am using to populate the GUI portion of the code:
I extract all the QPushButtons into a QList and iterate to access each one of
them (and that works fine the first time), but not when DestroyWindow()
within NewGame() is invoked....
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close()));
connect(ui->actionNew_Game, SIGNAL(triggered()), this, SLOT(NewGame()));
connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(About()));
InitiateWindow();
}
// actionNew_Game Signal handler
// Attempts to destroy the current puzzle and generate a new one
// NOTE: TO BE DEBUGGED, NOT FULLY FUNCTIONAL AS OF YET
void MainWindow::NewGame()
{
DestroyWindow();
InitiateWindow();
}
// Initializes the puzzle by calling an instance of Board and its puzzle
// generating functions. A list of all available QPushButtons is then
// iterated though, where every single button gets a number from the
// generated puzzle and background colour (either teal or white) depending
// whether its location is odd or even.
void MainWindow::InitiateWindow()
{
board = new Board();
board->Initialize();
board->GeneratePuzzle();
// board->DisplayInConsole();
std::vector<std::vector<int> > puzzle = board->getBoard();
QList<QPushButton*> allbuttons = this->findChildren<QPushButton*>();
int counter = 0;
int x =0; int y = 0;
int colorcount = 0;
foreach (QPushButton* button, allbuttons)
{
QString buttonname = "pushButton_" + QString::number(counter);
button->setObjectName(buttonname);
if (colorcount%2 == 0)
button->setStyleSheet("background-color: teal");
else
button->setStyleSheet("background-color: white");
if (puzzle[x][y] != -1)
button->setText(QString::number(puzzle[x][y]));
counter++,y++;colorcount++;
if (counter == board->getBoardlength())
{
x++;
y = 0;
counter = 0;
}
}
}
// Attempts to destory the current puzzle instance
// NOTE: TO BE DEBUGGED, NOT FULLY FUNCTIONAL AS OF YET
void MainWindow::DestroyWindow()
{
QString emptycell = " ";
board->~Board();
QList<QPushButton*> allbuttons = this->findChildren<QPushButton*>();
foreach (QPushButton* button, allbuttons)
button->setText(emptycell);
}
// default destructor
MainWindow::~MainWindow()
{
delete ui;
}
Any ideas? Any help would be appreciated!
Thanks!
I've been working on a GUI for a simple puzzle game where the program generates
numbers for a 9x9 grid. I've represented that grid as 81 different, stylized QPushButtons
and everything works so far. When the numbers are generated, each appear in its
allocated slot. The problem occurs when the user clicks the "New Game" Menu action
button (which is supposed to "reset" the grid and generate new random numbers); the
QPushButtons retain their text and do not "reset".
Here's the code I am using to populate the GUI portion of the code:
I extract all the QPushButtons into a QList and iterate to access each one of
them (and that works fine the first time), but not when DestroyWindow()
within NewGame() is invoked....
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close()));
connect(ui->actionNew_Game, SIGNAL(triggered()), this, SLOT(NewGame()));
connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(About()));
InitiateWindow();
}
// actionNew_Game Signal handler
// Attempts to destroy the current puzzle and generate a new one
// NOTE: TO BE DEBUGGED, NOT FULLY FUNCTIONAL AS OF YET
void MainWindow::NewGame()
{
DestroyWindow();
InitiateWindow();
}
// Initializes the puzzle by calling an instance of Board and its puzzle
// generating functions. A list of all available QPushButtons is then
// iterated though, where every single button gets a number from the
// generated puzzle and background colour (either teal or white) depending
// whether its location is odd or even.
void MainWindow::InitiateWindow()
{
board = new Board();
board->Initialize();
board->GeneratePuzzle();
// board->DisplayInConsole();
std::vector<std::vector<int> > puzzle = board->getBoard();
QList<QPushButton*> allbuttons = this->findChildren<QPushButton*>();
int counter = 0;
int x =0; int y = 0;
int colorcount = 0;
foreach (QPushButton* button, allbuttons)
{
QString buttonname = "pushButton_" + QString::number(counter);
button->setObjectName(buttonname);
if (colorcount%2 == 0)
button->setStyleSheet("background-color: teal");
else
button->setStyleSheet("background-color: white");
if (puzzle[x][y] != -1)
button->setText(QString::number(puzzle[x][y]));
counter++,y++;colorcount++;
if (counter == board->getBoardlength())
{
x++;
y = 0;
counter = 0;
}
}
}
// Attempts to destory the current puzzle instance
// NOTE: TO BE DEBUGGED, NOT FULLY FUNCTIONAL AS OF YET
void MainWindow::DestroyWindow()
{
QString emptycell = " ";
board->~Board();
QList<QPushButton*> allbuttons = this->findChildren<QPushButton*>();
foreach (QPushButton* button, allbuttons)
button->setText(emptycell);
}
// default destructor
MainWindow::~MainWindow()
{
delete ui;
}
Any ideas? Any help would be appreciated!
Thanks!