ouekah
29th March 2010, 15:26
Hi,
I use a QTableWidget to display a matrix. This matrix is subject to change over time and consequently so is the display.
I always use the same function to update the display: "updateTable"
The latter takes a list of names and a SquareMatrix object (that I created my self) as arguments.
My problem is the following:
everything goes fine when the display is updated for the first time, but the application crashes on subsequent updates. I checked my function and the problems seems to come from the setHorizontalHeaderItem/setVerticalHeaderItem functions.
Here is the code of "updateTable":
void MatrixDialog::updateTable(QStringList& names, SquareMatrix& matrix) {
int dimension = matrix.getDimension();
double value;
this->mapleEdit->setText(matrix.toMapleFormat());
this->mapleEdit->setCursorPosition(0);
this->matlabEdit->setText(matrix.toMatlabFormat());
this->matlabEdit->setCursorPosition(0);
this->matrixTable->clear();
this->matrixTable->setRowCount(matrix.getRows());
this->matrixTable->setColumnCount(matrix.getColumns());
for(int row = 0; row < dimension; ++row) {
QTableWidgetItem *headerItem = new QTableWidgetItem(names.at(row));
matrixTable->setHorizontalHeaderItem(row, headerItem);
matrixTable->setVerticalHeaderItem(row, headerItem);
for(int column = 0; column < dimension; ++column) {
matrix.getElement(row, column, value);
QTableWidgetItem *item = new QTableWidgetItem(QString::number(value));
item->setTextAlignment(Qt::AlignCenter);
this->matrixTable->setItem(row, column, item);
}
}
}
If I comment the calls to these functions, the application doesn't crash anymore and the values are correctly updated.
void MatrixDialog::updateTable(QStringList& names, SquareMatrix& matrix) {
int dimension = matrix.getDimension();
double value;
this->mapleEdit->setText(matrix.toMapleFormat());
this->mapleEdit->setCursorPosition(0);
this->matlabEdit->setText(matrix.toMatlabFormat());
this->matlabEdit->setCursorPosition(0);
this->matrixTable->clear();
this->matrixTable->setRowCount(matrix.getRows());
this->matrixTable->setColumnCount(matrix.getColumns());
for(int row = 0; row < dimension; ++row) {
QTableWidgetItem *headerItem = new QTableWidgetItem(names.at(row));
// matrixTable->setHorizontalHeaderItem(row, headerItem);
// matrixTable->setVerticalHeaderItem(row, headerItem);
for(int column = 0; column < dimension; ++column) {
matrix.getElement(row, column, value);
QTableWidgetItem *item = new QTableWidgetItem(QString::number(value));
item->setTextAlignment(Qt::AlignCenter);
this->matrixTable->setItem(row, column, item);
}
}
}
This might be related to
this->matrixTable->clear(); but I'm not sure. Could someone explain to me what's going on and what should I do to prevent these crashes from happening ?
I use a QTableWidget to display a matrix. This matrix is subject to change over time and consequently so is the display.
I always use the same function to update the display: "updateTable"
The latter takes a list of names and a SquareMatrix object (that I created my self) as arguments.
My problem is the following:
everything goes fine when the display is updated for the first time, but the application crashes on subsequent updates. I checked my function and the problems seems to come from the setHorizontalHeaderItem/setVerticalHeaderItem functions.
Here is the code of "updateTable":
void MatrixDialog::updateTable(QStringList& names, SquareMatrix& matrix) {
int dimension = matrix.getDimension();
double value;
this->mapleEdit->setText(matrix.toMapleFormat());
this->mapleEdit->setCursorPosition(0);
this->matlabEdit->setText(matrix.toMatlabFormat());
this->matlabEdit->setCursorPosition(0);
this->matrixTable->clear();
this->matrixTable->setRowCount(matrix.getRows());
this->matrixTable->setColumnCount(matrix.getColumns());
for(int row = 0; row < dimension; ++row) {
QTableWidgetItem *headerItem = new QTableWidgetItem(names.at(row));
matrixTable->setHorizontalHeaderItem(row, headerItem);
matrixTable->setVerticalHeaderItem(row, headerItem);
for(int column = 0; column < dimension; ++column) {
matrix.getElement(row, column, value);
QTableWidgetItem *item = new QTableWidgetItem(QString::number(value));
item->setTextAlignment(Qt::AlignCenter);
this->matrixTable->setItem(row, column, item);
}
}
}
If I comment the calls to these functions, the application doesn't crash anymore and the values are correctly updated.
void MatrixDialog::updateTable(QStringList& names, SquareMatrix& matrix) {
int dimension = matrix.getDimension();
double value;
this->mapleEdit->setText(matrix.toMapleFormat());
this->mapleEdit->setCursorPosition(0);
this->matlabEdit->setText(matrix.toMatlabFormat());
this->matlabEdit->setCursorPosition(0);
this->matrixTable->clear();
this->matrixTable->setRowCount(matrix.getRows());
this->matrixTable->setColumnCount(matrix.getColumns());
for(int row = 0; row < dimension; ++row) {
QTableWidgetItem *headerItem = new QTableWidgetItem(names.at(row));
// matrixTable->setHorizontalHeaderItem(row, headerItem);
// matrixTable->setVerticalHeaderItem(row, headerItem);
for(int column = 0; column < dimension; ++column) {
matrix.getElement(row, column, value);
QTableWidgetItem *item = new QTableWidgetItem(QString::number(value));
item->setTextAlignment(Qt::AlignCenter);
this->matrixTable->setItem(row, column, item);
}
}
}
This might be related to
this->matrixTable->clear(); but I'm not sure. Could someone explain to me what's going on and what should I do to prevent these crashes from happening ?