PDA

View Full Version : QTableWidget and the setHorizontalHeaderItem/setVerticalHeaderItem functions



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 ?

Lykurg
29th March 2010, 20:57
If your matrix don't have different sizes and names you could use QTableWidget::clearContents().

What crashes your app is that if dimension is lesser than matrix.getColumns() or matrix.getRows(). That the only reason I can see right now.

ouekah
29th March 2010, 21:16
Unfortunately the matrix's size may vary over time... And dimension points to the same value as rows and columns in SquareMatrix... I feel sad :(

Lykurg
30th March 2010, 06:59
And dimension points to the same value as rows and columns in SquareMatrix...
I thought so because of the name. But you never know here... So only thing I can guess of (without proving it) is, that you cant reuse one item for vertical and horizontal header. So try:
matrixTable->setHorizontalHeaderItem(row, new QTableWidgetItem(names.at(row)));
matrixTable->setVerticalHeaderItem(row, new QTableWidgetItem(names.at(row)));


If that is also not helping, debug and backtrace to see where it exactly crashes.

ouekah
30th March 2010, 23:40
I read the documentation again and I noticed this function: "QTableWidget::setHorizontalHeaderLabels".

It's even easier with that one and there is no more crash... :cool:

borzh62
29th October 2015, 17:25
I read the documentation again and I noticed this function: "QTableWidget::setHorizontalHeaderLabels".

It's even easier with that one and there is no more crash... :cool:

Row/column indexes start from 1. You should use next code:



matrixTable->setHorizontalHeaderItem(row+1, headerItem);
matrixTable->setVerticalHeaderItem(row+1, headerItem);

ChrisW67
29th October 2015, 19:57
Row/column indexes start from 1.
No, no they don't.

I also note that if the names list ever contains fewer names than the dimension of the matrix, deliberately or by accident, then the calls to QStringList::at() will fail.