[PyQt] About dimensions of QAbstractTableModel

Andreas Pakulat apaku at gmx.de
Mon Nov 29 22:51:18 GMT 2010

On 29.11.10 23:03:10, Vicent Mas wrote:
> On 2010-11-29 Andreas Pakulat <apaku at gmx.de> said:
> > On 29.11.10 18:48:26, Vicent Mas wrote:
> > > 2010/11/28 Andreas Pakulat <apaku at gmx.de>:
> > > > On 27.11.10 21:27:55, Ian wrote:
> > One such thing is that rowCount can be used
> > to implement hasChildren, which is what is being done in the default
> > implementations. In such a case rowCount is called with an invalid index
> > to get the toplevel rowcount. Then when the view wants to find out
> > wether an index has children its again called with the valid top-level
> > index. In such a case a table and list model wants to make sure to
> > return 0 as it has no childrens. This detail is actually also mentioned
> > in the API docs for QAbstractItemView::rowCount() as tip.
> > 
> Indeed that's a very interesting info. But for table models I think that 
> hasChildren is not meaningful. One can overwrite it without calling rowCount 
> and then use safely the approach used in the Summerfield's book that I mention 
> in my previous mail. I know that Qt api docs recommend something different, but 
> the Summerfield's approach is simpler and it just works.

I don't know Marks book, but I do know that his material is high
quality. Sure for a simple example to learn the basics returning a
static number regardless of the parent item passed in is fine. Its
easier to understand for beginners, but that doesn't mean its correct.
And once your model gets more complicated or you use it for different
views you'll eventually run into behavioural weirdness. At that point
you should verify that your model works sanely (according to what Qt's
itemviews define as sane, which may not be the same as the definition of
a human brain ;). Best/easiest way to do that is adding the mentioned
modeltest to your model and run your app. One of the things that it'll
instantly assert on is returning non-0 amount of rows for a valid
parent but no index/data for it.


Don't Worry, Be Happy.
		-- Meher Baba

More information about the PyQt mailing list