C++QT How to change size grid when index in qcombobox is changed
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...
Code:
void MatrixOP::createGridMatrixSize()
{
setMatrixSize
=new QGroupBox(tr
("Select Size"));
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"));
for(int j=1; j<=gridSize;j++){
for (int i=1; i<=gridSize;i++){
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
Re: C++QT How to change size grid when index in qcombobox is changed
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,
_
Re: C++QT How to change size grid when index in qcombobox is changed
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..
Code:
connect(chooseSize, SIGNAL(activated(int)), this, SLOT(on_changeIndex_chooseSize(int)));
mainLayout->addWidget(setMatrixSize);
mainLayout->addWidget(setMatrixValues);
mainLayout->addWidget(chooseSize);
setLayout(mainLayout);
void MatrixOP::createGridMatrixValues(int size)
{
setMatrixValues
= new QGroupBox (tr
("Input Values"));
for(int j=1; j<=size;j++){
for (int i=1; i<=size;i++){
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?
Re: C++QT How to change size grid when index in qcombobox is changed
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,
_
Re: C++QT How to change size grid when index in qcombobox is changed
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...
Code:
void MatrixOP::createGridMatrixValues(int size)
{
setMatrixValues
= new QGroupBox (tr
("Input Values"));
qDebug()<<size;
for(int j=1; j<=size+1;j++){
QVector<QLineEdit *> foo;
for (int i=1; i<=size+1;i++){
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);
}
Re: C++QT How to change size grid when index in qcombobox is changed
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,
_