PDA

View Full Version : C++QT How to change size grid when index in qcombobox is changed



danalex07
4th October 2015, 02:11
im trying to create a matrix grid that changes its size when an option in the combobox is selected, i dont know how to call the function that creates the grid everytime i change the value of the combo box...




void MatrixOP::createGridMatrixSize()
{
setMatrixSize=new QGroupBox(tr("Select Size"));
QGridLayout *layout =new QGridLayout;
QLabel *sizeLabel = new QLabel(tr("Size"));
QComboBox *chooseSize = new QComboBox;
chooseSize->addItems(QStringList()<<"0"<<"1"<<"2"<<"3"<<"4"<<"5"<<"6"<<"7"<<"8"<<"9"<<"10");
chooseSize->setCurrentIndex(1);
layout->addWidget(sizeLabel, 0,0);
layout->addWidget(chooseSize, 0,1);
setMatrixSize->setLayout(layout);
}

void MatrixOP::createGridMatrixValues()
{
int gridSize=chooseSize->currentIndex();
setMatrixValues = new QGroupBox (tr("Input Values"));
QGridLayout *layout = new QGridLayout;
for(int j=1; j<=gridSize;j++){
for (int i=1; i<=gridSize;i++){
matrixValues[i]=new QLineEdit;
layout->addWidget(matrixValues[i], j, i+1);
}
}
setMatrixValues->setLayout(layout);

}

i believe it can be done with SIGNALS and SLOTS but i cant figure out the way to refresh the mainwindow to "redraw" the grid....any help?thx

anda_skoa
4th October 2015, 11:38
If you make createGridMatrixValues() a slot, you can connect it to the chooseSize combobox's change signals, e.g. currentIndexChanged.

In creteGridMatrixValues() you first delete the current setMatrixValues widgets, create it anew using your new input and then re-add it to its parent's layout.

Cheers,
_

danalex07
10th October 2015, 03:07
i cant make it work, im trying to delete the array of widgets when the slot is called, it compiles but when i change the value in the combo box y get a crash..



connect(chooseSize, SIGNAL(activated(int)), this, SLOT(on_changeIndex_chooseSize(int)));

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(setMatrixSize);
mainLayout->addWidget(setMatrixValues);
mainLayout->addWidget(chooseSize);
setLayout(mainLayout);


void MatrixOP::createGridMatrixValues(int size)
{
setMatrixValues = new QGroupBox (tr("Input Values"));
QGridLayout *layout = new QGridLayout;
matrixValues=new QLineEdit* [size];
for(int j=1; j<=size;j++){
for (int i=1; i<=size;i++){
matrixValues[i]=new QLineEdit[size];
layout->addWidget(matrixValues[i], j, i+1);
}
}

setMatrixValues->setLayout(layout);

}

void MatrixOP::on_changeIndex_chooseSize(int index)
{
for (int i=0;i<matrixSize;i++){
delete [] matrixValues[i];
}
delete[]matrixValues;
createGridMatrixValues(index);
}


any helps?

anda_skoa
10th October 2015, 10:54
You are using the delete[] operator on an normal pointer inside the loop.

If you use a QVector instead of a plain C array, you can just use qDeleteAll() on the container and then clear() it.

Cheers,
_

danalex07
11th October 2015, 22:03
thanks for the help, i change my array w/ a Qvector<qvector<qlineEdit>> array but i still cant figure out how to use qDeleteAll() to remove the created widgets and add a new ones...




void MatrixOP::createGridMatrixValues(int size)
{
setMatrixValues = new QGroupBox (tr("Input Values"));
QGridLayout *layout = new QGridLayout;
qDebug()<<size;
for(int j=1; j<=size+1;j++){
QVector<QLineEdit *> foo;
for (int i=1; i<=size+1;i++){
QLineEdit *value =new QLineEdit(this);
foo.append(value);
layout->addWidget(value, j, i);
}
vectorOfVectorOfLineEdits.append(foo);
}

setMatrixValues->setLayout(layout);
}
void MatrixOP::on_changeIndex_chooseSize(int index)
{
vectorOfVectorOfLineEdits.clear();
createGridMatrixValues(index);
}

anda_skoa
12th October 2015, 00:51
Well, one thing that is definitely wrong is creation of the groupbox every time.
Also missing the call to delete the layout.

As for qDeleteAll(), since you have a vector of vectors, you'll need to iterate over the outer vector and call qDeleteAll on each element.
qDeleteAll() iterates over that inner vector then an calls delete on each element.

Cheers,
_