[PyQt] QTreeView: inserting and removing rows in view and underlying data...

Andreas Pakulat apaku at gmx.de
Fri Nov 9 10:38:19 GMT 2007

On 09.11.07 10:02:31, Mark Summerfield wrote:
> On 2007-11-09, Dirk Wagener wrote:
> > Hi
> >
> > I am experiencing some confusion regarding the model/view architecture
> > when using a QTreeView with a custom
> > QAbstractItemModel.
> [snip]
> > Can somebody please give me some advice. Where can I learn how to do
> > this model/view interaction properly?
> > Are there some rules which I am not obeying here??
> I find it difficult to answer without seeing more code. However, some
> things are clear:
> - If you insert rows you _must_ do so in an insertRows()

No, you can use any function in your model class, the important bit is:

>   reimplementation and you _must_ call beginInsertRows() (with the
>   correct arguments) before inserting, and endInsertRows() at the end.
>   Similarly for removing rows.

If you always keep the begin/end then everything will work fine. In fact
insertRows() is only needed if you use the function or use insertRow()
from the QAbstractItemModel API. Which is of course true if you have a
view that allows for inserting rows by clicking an empty area or some

> - You should never need to emit layout-related signals. Inside your
>   setData() reimplementation you _must_ emit the dataChanged() signal
>   (if data is actually changed); if you do lots of changes, then you
>   should call QAbstractItemModel.reset()---this will tell any views that
>   they must refresh all their visible data.

Well, if I create a sorting model I think I need to emit the
layout-related signals.


You have the body of a 19 year old.  Please return it before it gets wrinkled.

More information about the PyQt mailing list