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":

Qt Code:
  1. void MatrixDialog::updateTable(QStringList& names, SquareMatrix& matrix) {
  2. int dimension = matrix.getDimension();
  3. double value;
  4.  
  5. this->mapleEdit->setText(matrix.toMapleFormat());
  6. this->mapleEdit->setCursorPosition(0);
  7. this->matlabEdit->setText(matrix.toMatlabFormat());
  8. this->matlabEdit->setCursorPosition(0);
  9.  
  10. this->matrixTable->clear();
  11. this->matrixTable->setRowCount(matrix.getRows());
  12. this->matrixTable->setColumnCount(matrix.getColumns());
  13.  
  14. for(int row = 0; row < dimension; ++row) {
  15. QTableWidgetItem *headerItem = new QTableWidgetItem(names.at(row));
  16. matrixTable->setHorizontalHeaderItem(row, headerItem);
  17. matrixTable->setVerticalHeaderItem(row, headerItem);
  18. for(int column = 0; column < dimension; ++column) {
  19. matrix.getElement(row, column, value);
  20. QTableWidgetItem *item = new QTableWidgetItem(QString::number(value));
  21. item->setTextAlignment(Qt::AlignCenter);
  22. this->matrixTable->setItem(row, column, item);
  23. }
  24. }
  25. }
To copy to clipboard, switch view to plain text mode 

If I comment the calls to these functions, the application doesn't crash anymore and the values are correctly updated.

Qt Code:
  1. void MatrixDialog::updateTable(QStringList& names, SquareMatrix& matrix) {
  2. int dimension = matrix.getDimension();
  3. double value;
  4.  
  5. this->mapleEdit->setText(matrix.toMapleFormat());
  6. this->mapleEdit->setCursorPosition(0);
  7. this->matlabEdit->setText(matrix.toMatlabFormat());
  8. this->matlabEdit->setCursorPosition(0);
  9.  
  10. this->matrixTable->clear();
  11. this->matrixTable->setRowCount(matrix.getRows());
  12. this->matrixTable->setColumnCount(matrix.getColumns());
  13.  
  14. for(int row = 0; row < dimension; ++row) {
  15. QTableWidgetItem *headerItem = new QTableWidgetItem(names.at(row));
  16. // matrixTable->setHorizontalHeaderItem(row, headerItem);
  17. // matrixTable->setVerticalHeaderItem(row, headerItem);
  18. for(int column = 0; column < dimension; ++column) {
  19. matrix.getElement(row, column, value);
  20. QTableWidgetItem *item = new QTableWidgetItem(QString::number(value));
  21. item->setTextAlignment(Qt::AlignCenter);
  22. this->matrixTable->setItem(row, column, item);
  23. }
  24. }
  25. }
To copy to clipboard, switch view to plain text mode 

This might be related to
Qt Code:
  1. this->matrixTable->clear();
To copy to clipboard, switch view to plain text mode 
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 ?