PDA

View Full Version : How do i update this grid of buttons?



BrotherZer0
21st May 2018, 07:53
I have an n and m as two global variables and I wish to generate a grid of buttons size n*m. I have a combo box which changes n and m if the option is changed. My question is how do I update the grid?

Here is my code:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QPushButton"
#include "QGridLayout"
#include "QLabel"
#include "QComboBox"
#include "QDebug"

int n=9,m=9;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QWidget *centralWidget = new QWidget;
int count=1,i,j;

QPushButton *button[10][10];
QGridLayout *controlsLayout = new QGridLayout;
QGridLayout *controlsLayout1 = new QGridLayout;
QPushButton *Reset=new QPushButton;
QLabel *Bombs=new QLabel;
QLabel *Timer=new QLabel;

Timer->setText("timer");
Bombs->setText("bombs");

QStringList commands = { "Easy", "Medium", "Hard", "Custom" };
QComboBox* combo = new QComboBox(this);
combo->addItems(commands);
connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(commandChanged(int)));

controlsLayout->addWidget(combo,0,0,1,2);
controlsLayout->addWidget(Bombs,1,0);
controlsLayout->addWidget(Reset,1,4);
controlsLayout->addWidget(Timer,1,8);

Reset->setMinimumSize(40,40);
Reset->setMaximumSize(40,40);

for(i=2;i<n;i++)
{
for(j=0;j<m;j++)
{
if(count<=100)
{

button[i][j] = new QPushButton();
button[i][j]->setMinimumSize(40,40);
button[i][j]->setMaximumSize(40,40);
button[i][j]->move(40*j, 40*i);
button[i][j]->show();
controlsLayout->addWidget(button[i][j], i, j);
count++;
}
}
}
controlsLayout->setSpacing(5);
centralWidget->setLayout(controlsLayout);
setCentralWidget(centralWidget);
}
void MainWindow::commandChanged(int index)
{
if (index==0){n=9;m=9;}
else{n=9;m=9;}

qDebug() << n<<" "<<m;

}
MainWindow::~MainWindow()
{
delete ui;
}

d_stranz
23rd May 2018, 16:33
You start by moving almost all of the code you have in the MainWindow constructor into a separate method, something like "buildButtonGrid( int n, int m )". Then you rewrite the code to get rid of all of the hard-coded constants (like "10", "100", and so forth) and replace them with "n", "m", "n + 1", "m + 1", etc. as appropriate. You also get rid of the hard-coded "QPushButton * button[10][10]" and replace it with a class member variable "QVector< QButton * > buttons" or something similar that you resize as needed to hold n*m button pointers.

Finally, you get rid of the absolute positioning ("move( 40 * j, 40 * i)") because the grid control will put the buttons in the necessary places.

When you resize the grid, be sure you also remember to delete the previous buttons.

This piece of code shows that you have a logical error in your thinking, because it looks like you had a problem with indexing into your button array and don't understand why. That's why you had to add the "count" variable. You need to rethink this.


for(i=2;i<n;i++)
{
for(j=0;j<m;j++)
{
if(count<=100)
{

button[i][j] = new QPushButton();
button[i][j]->setMinimumSize(40,40);
button[i][j]->setMaximumSize(40,40);
button[i][j]->move(40*j, 40*i);
button[i][j]->show();
controlsLayout->addWidget(button[i][j], i, j);
count++;
}
}
}