PDA

View Full Version : Quick Question:Why my TableView appears blank?!



fatecasino
4th March 2011, 00:00
MyDialog::MyDialog(QWidget *parent)
: QWidget(parent)
{
QStandardItemModel model(4, 2);
tableView = new QTableView(this);
tableView->setModel(&model);

ComboBoxDelegate delegate;
tableView->setItemDelegateForColumn(1,&delegate);

for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 2; ++column) {
QModelIndex index = model.index(row, column, QModelIndex());
model.setData(index, QVariant((row+1) * (column+1)));
}
}
view2 = new QTableView(this);
view2->setModel(&model);
view2->setItemDelegateForColumn(1,&delegate);

for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 2; ++column) {
QModelIndex index = model->index(row, column, QModelIndex());
model->setData(index, QVariant((row+1) * (column+1)));
}
}

vboxlayout = new QVBoxLayout(this);
vboxlayout->addWidget(tableView);
vboxlayout->addWidget(view2);

setLayout(vboxlayout);
}

Added after 1 12 minutes:

found it!!

In the header file I have:

private:
QStandardItemModel* model;

In the source I had written:


QStandardItemModel model(4, 2);
tableView->setModel(&model);

BUT it should be:



model = new QStandardItemModel(4, 2);
tableView->setModel(model);

ChrisW67
4th March 2011, 06:30
You have the same problem with your ComboBoxDelegate going out of scope at the end of the constructor.

fatecasino
4th March 2011, 12:06
You have the same problem with your ComboBoxDelegate going out of scope at the end of the constructor.
you are totally right!