PDA

View Full Version : How to notify QTableView of a modified CustomModel



mstegehu
23rd February 2010, 16:55
Hello,

I was wondering if there is a way to have the QTableView reread the model data.

If I set a custom model on a tableview all data is read. But if I after that add data, this is due to loading of data, to the custom model without using beginInsertRows() and endInsertRows() there seems to be no way of letting the View know that the whole model needs to be reread.

If I set the model on the view after loading the data everything works fine. But than the programs does not work in other situations.

Does anyone know how to force a total update of the TableView?

Regards,

Marcel

bmhautz
23rd February 2010, 17:21
You can always reset the model for the view. Notice though that any signals and slots will need to be reconnected and the memory for the old model deleted (pseudocode):

CustomModel* oldModel = treeviewPtr->getModel();
CustomModel* temp = new CustomModel();
treeviewPtr->setModel(temp);
delete oldModel;

pascal123
23rd February 2010, 21:16
What I would do here is first delete all rows (using beginRemoveRows/endRemoveRows), and then add the new rows as you did.

stefanadelbert
24th February 2010, 05:35
Call beginResetModel; modify model; then call endResetModel. That should do the trick. It shouldn't be necessary to delete and replace the model.

bmhautz
24th February 2010, 16:42
Call beginResetModel; modify model; then call endResetModel. That should do the trick. It shouldn't be necessary to delete and replace the model.

That would work, too, but only if you're using 4.6 and above.

stefanadelbert
24th February 2010, 23:33
Ah, right. I have only recently started using Qt so only know 4.6+. I'm not going to be much help here.

bmhautz
25th February 2010, 00:06
I think it was a great thing that you mentioned it! I didn't even know it existed until I looked at the recent Qt documentation for 4.6.x. I'm currently using 4.5.3, so I had to use the setModel, delete old model pointer method for my uses. But I'll be sure to use the new method you mentioned when I upgrade!

graciano
25th February 2010, 00:24
Hi,

I solved this kind of "situation" in a very "craft" way:



...
Dialog::Dialog(QWidget *parent)
: QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->fileNameLabel->setText("pessoa.png");
model = new QSqlTableModel(this);
setModel();
setView();
...
}

void Dialog::setModel()
{
model->setTable("photo");
model->setHeaderData(photo_id, Qt::Horizontal, tr("ID"));
model->setHeaderData(photo_name, Qt::Horizontal, trUtf8("Filename"));
model->setHeaderData(photo_image, Qt::Horizontal, trUtf8("Image"));
model->select();
}

void Dialog::setView()
{
view = ui->tableView;
view->setModel(model);
view->setColumnHidden(photo_image, true);
view->horizontalHeader()->setStretchLastSection(true);
view->setSelectionMode(QAbstractItemView::SingleSelectio n);
view->setSelectionBehavior(QAbstractItemView::SelectRows );
}


This way i call setView() when i need to make sure that model and view are sinchronized.