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

Mark Summerfield mark at qtrac.eu
Fri Nov 9 10:02:31 GMT 2007

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.
> 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()
  reimplementation and you _must_ call beginInsertRows() (with the
  correct arguments) before inserting, and endInsertRows() at the end.
  Similarly for removing rows.
- 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.

Getting hierarchical models working is tricky. You might find more help
in the C++ documentation:

Mark Summerfield, Qtrac Ltd., www.qtrac.eu

More information about the PyQt mailing list