PDA

View Full Version : Change the Model via the View



thw
25th September 2006, 14:55
Hello all together,

currently I'm just learning programming with Qt. Today I found a problem that I cannot solve by myself :(

I have an (empty) QStandardItemModel that is shown with a QTableView. After the user did something, I want to fill the model with some data, that means I want to change it (e.g. add some more rows or set new data). Because I don't have the pointer to the model itself anymore, I want to get the pointer out of the TableView:
QStandardItemModel* model=myTable->model();
Now I only get a pointer with type QAbstractItemModel, which is an abstract class and misses some members of QStandardItemView. Casting it to QStandardItemView* seems to work: QStandardItemModel* model=(QStandardItemView*)myTable->model();
But now my program crashes with a segmentation fault on every call like
QModelIndex index=model->index(0, 0, QModelIndex());
or
model->insertRows(0, Count(), QModelIndex());

What am I doing wrong? Is the crash related to the cast or to the invalid QModelIndex?

Thank you for help,
thw

wysota
25th September 2006, 15:13
QModelIndex index=model->index(0, 0, QModelIndex());
This shouldn't crash, no matter what, provided that model!=0 (and it shouldn't be, if you set the model for the view using setModel).

BTW. You don't need to have access to any of QStandardItemModel methods, you can operate directly on QAbstractItemModel.