PDA

View Full Version : Instantiate QTableWidget dynamically during runtime



ouekah
19th March 2010, 17:00
Hi guys,

I'm developing an application which carries a matrix and I would like to allow the user to display that matrix on demand. I use a QTableWidget within a QDialog to do that.

When I launch the application the QDialog is created but cannot be displayed (I don't want to...) until a first matrix is computed. At that time the QTableWidget is not instantiated yet. My idea is to instantiate the table only once the first matrix computed (because then I will know the dimensions). If the matrix or its dimension are further modified, the table will be modified accordingly (but not instantiated again for efficiency purposes).

Apparently there is a problem with my philosophy, because when I run the application, it suddenly crashes when it executes:



this->matrixTable = new QTableWidget(size, size, this);


And I don't understand why... Could somebody explain me why I can't do that and hopefully also tell me how to proceed then ? :)

JD2000
19th March 2010, 19:39
I think that you want a QTableView to do this.

Base it on a model and use the dataChanged() or layoutChanged() signals to achieve what you are after.

Lykurg
19th March 2010, 19:44
I guess matrixTable is your original table widget. So there is no need to create a new one. just clear the existing and reuse it.

ouekah
20th March 2010, 17:54
I modified my dialog but my program keeps crashing and I don't find the root cause. :(

My application is defined as follows:

there are 3 classes:

Dummy, Coordinator and MatrixDialog

Dummy performs computation to generate the matrix I need. This matrix is then sent to the Coordinator which forwards it to the MatrixDialog.

here is the code of MatrixDialog:

matrixDialog.h



class MatrixDialog : public QDialog {
Q_OBJECT

public:
MatrixDialog(QString windowTitle, QWidget *parent = 0);
void updateTable(QStringList& strings, SquareMatrix& matrix);


private slots:
void ok();

private:
QTableWidget *matrixTable;
};


matrixDialog.cpp



#include "matrixDialog.h"

MatrixDialog::MatrixDialog(QString windowTitle, QWidget *parent) : QDialog(parent) {
this->matrixTable = new QTableWidget(this);
this->matrixTable->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
this->matrixTable->horizontalHeader()->setStretchLastSection(true);
this->matrixTable->verticalHeader()->setStretchLastSection(true);

QHBoxLayout *formatLayout = new QHBoxLayout;
formatLayout->addWidget(mapleLabel);
formatLayout->addWidget(mapleEdit);

QPushButton *okButton = new QPushButton("OK");
okButton->setFixedSize(okButton->size());
connect(okButton, SIGNAL(clicked()), this, SLOT(ok()));

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(matrixTable);
mainLayout->addWidget(okButton);

setLayout(mainLayout);

setWindowTitle(windowTitle);
}

void MatrixDialog::ok() {
this->hide();
}

void MatrixDialog::updateTable(QStringList& tickers, SquareMatrix& matrix) {
int dimension = matrix.getDimension();
double value;

this->matrixTable->clear();
this->matrixTable->setRowCount(matrix.getRows());
this->matrixTable->setColumnCount(matrix.getColumns());

for(int row = 0; row < dimension; ++row) {
QTableWidgetItem *headerItem = new QTableWidgetItem(tickers.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);
}
}
}


When the Coordinator calls updateTable of MatrixDialog, the application crashes. I even deleted all updateTable's content, but the application still crashes !

Coordinator is defined as follows:



class Coordinator : public QObject {
Q_OBJECT
...
};



I'm lost...I just don't understand how I can do an unauthorized access... I would really appreciate some help !

Thanks guys in advance for your support

Lykurg
20th March 2010, 18:08
Run a debugger and see where it crashes exactly. I guess its on tickers.at(row) are you sure your string list is as long as your dimension?

Lykurg
20th March 2010, 18:10
matrixTable->setHorizontalHeaderItem(row, headerItem);
matrixTable->setVerticalHeaderItem(row, headerItem); And this is also buggy! if matrix.getRows() > matrix.getColums()

ouekah
20th March 2010, 18:49
I believe the issue is somewhere else because if I delete all the instructions within updateTable, the application still crashes. But If the Coordinator doesn't call updateTable, the application runs ok...

Thank you so much for your help, I really appreciate...

Lykurg
20th March 2010, 19:10
Then how the Coordinator is calling the member function of MatrixDialog. Have you check the pointer?

ouekah
21st March 2010, 11:31
I solved the issue finally ! It was not a Qt issue, but a pointer issue :rolleyes:...(shame on me) ^^