[Qt-interest] Notification of moving an item in a model

Stephen Kelly steveire at gmail.com
Wed Feb 18 13:19:36 CET 2009


Hi,

Hi,

I'm implementing a tree-like model of Collections and Items. Collections can 
contain Items and other Collections. I am also implementing a proxy model to 
represent that tree as a flat list. The image on the left here is the tree and 
the image in the center is the proxied model. The text should read 
DescendantEntitiesProxyModel, but I haven't changed it yet.

http://api.kde.org/playground-api/pim-
apidocs/kjotsrewrite/akonadi_next/html/howtousedem.png

I am able to notifiy the proxy model when rows in the model are removed or 
inserted, but I have no way of notifying the proxy that an item is to be 
moved. 

Notifications to change the model come from a monitor object, so I have 
something like:
connect(monitor, SIGNAL(itemMoved(Item /* item */, Collection /* source */, 
Collection /* dest */)), SLOT(itemMoved(itemMoved(Item /* item */, Collection 
/* source */, Collection /* dest */)))

The itemMoved slot should probably look something like this:

  MyModelPrivate::itemMoved(Item item, Collection source, Collection dest)
  {
    q->layoutAboutToBeChanged();

    // move the item in the model.

    // Change some persistent indexes.

    q->layoutChanged();
  }

Initially I thought I should do something like

  q->beginRemoveRows();
  // remove from source.
  q->endRemoveRows();
  q->beginInsertRows();
  // insert at correct position.
  q->endInsertRows();

instead of changing the layout, but I think that would needlessly invalidate 
the persistent indexes of the moved Item or Collection and all descendants in 
the case of moving a Collection. I think it might also clear sections of a 
selection if the moved Collection or one of its descendants is moved. It could 
also be quite expensive with large models.

The documentation seems to say that emitting layout changed is the right way 
to do this kind of move operation anyway. However, I'm a bit confused about 
how it should work. I emit layoutChanged, but without specifying which items 
in the tree were moved, or which parents should have their children layed out 
again.

Should I just reset the proxy model each time the layout of the source model 
changes? That would cause a lot of resets I think, and many would be 
unnecessary for example reseting when a row is inserted.

The other option I can think of is to emit indexAboutToBeMoved and indexMoved 
signals from my tree model myself, and create a MyAbstractProxyModel subclass 
of QAbstractProxyModel which handles and reemits those signals, and then make 
sure that all proxy models in my system inherit from MyAbstractProxyModel. 

Can someone give me some guidance on this? Have I missed something in the 
docs?

All the best,

Steve.





More information about the Qt-interest-old mailing list