PDA

View Full Version : QTreeView and QAbstractionModel



The Storm
11th September 2007, 16:43
Hi all,

I've managed how to add rows and columns with data in them(its a bit complicated for me) but I can't understand how I can remove rows with all the data in them.
For example first how I can make the TreeView in that way that I can select multiple items/rows and then with clicking on buton to remove only the selected ones. If someone can give me some example and maybe where I can read more about this modelling and TreeView. I readed a lot in doc.trolltech.com but I still can't understand how all thoose things works.

Thanks. :)

jpn
11th September 2007, 17:05
Hi, you can adjust QAbstractItemView::selectionMode (http://doc.trolltech.com/4.3/qabstractitemview.html#selectionMode-prop) to make it possible to select multiple items in a view.

To remove items, you could implement QAbstractItemModel::removeRows() more or less like this:


bool MyModel::removeRows(int row, int count, const QModelIndex &parent)
{
// a sanity check
if (row < 0 || count <= 0 || (row + count) > rowCount(parent))
return false;

// inform model-view frame work about upcoming removal
beginRemoveRows(QModelIndex(), row, row + count - 1);

// remove corresponding items from the internal data structure in here

// inform model-view frame work about finished removal
endRemoveRows();

// removal succeed
return true;
}

Now removing a bunch of selected items is kind of problematic. You cannot just remove them in an arbitrary order from the model because removing a certain item might cause other indices to become invalid. The solution is to remove items in reverse order, from end to beginning.

The Storm
11th September 2007, 17:52
Is it needed to make new class, can I just use the Standart model for the construction? :)

jpn
11th September 2007, 18:30
Wait, which model are you using? I thought you already had QAbstractItemModel subclass.

The Storm
11th September 2007, 20:33
QAbstractItemModel *pModel = new QStandardItemModel(parent);
This is what I use, I saw it in one example. :)

wysota
11th September 2007, 21:47
So you should be able to retrieve selected rows through QItemSelectionModel of the tree view and remove them from the model.

jpn
11th September 2007, 21:48
Oh, you should've mentioned that you're using QStandardItemModel. So forget about reimplementing removeRows() then, QStandardItemModel already implements it. Just call it. :)

The Storm
11th September 2007, 22:04
Can you show me example how can I remove the selected rows? :)

wysota
11th September 2007, 22:28
Call removeRow() on the contents of QItemSelectionModel of the view.

The Storm
11th September 2007, 23:22
QTreeView doesn't have member that return QItemSelectionModel.

jpn
12th September 2007, 07:22
QTreeView is a QAbstractItemView. See QAbstractItemView::selectionModel().

The Storm
12th September 2007, 13:40
Ok is it bug in Qt 4.3.1 on Windows that when I remove row it is removed but its like the others become hidden and when I add new they show up again?

wysota
12th September 2007, 20:38
Not likely. Can we see the code you wrote?

The Storm
12th September 2007, 21:27
Oh, it was my mistake, I though that I must set the model each time when I change it, because I though that setModel() just copy the pointer but it seems its not like that, now all works fine, thanks a lot. :)