[PyQt] About dimensions of QAbstractTableModel

Andreas Pakulat apaku at gmx.de
Mon Nov 29 18:41:21 GMT 2010


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:
> 
> > [...]
> >
> >>     def rowCount(self, parent = None):
> >>         ''' return No of rows of data. parent is a QModelIndex '''
> >>         return len(self.view)
> >
> > This is wrong, even for table models you have to take care to return the
> > right number of rows depending on the parent. That means if your model
> > gets asked for the rowCount with a valid parent, you want to return 0
> > (as you don't have childs under any of your rows). So check for
> > parent.isValid().
> >
> >>     def columnCount(self, parent = None):
> >>         ''' return number of columns. parent = QModelIndex()
> >>            id, name, cubref, address, town, contacts
> >>         '''
> >>         return 6
> >
> > Basically the same here as above, though I think this is not quite as
> > critical.
> >
> 
> Hi Andreas,
> 
> Are you sure about this? Does it mean that the example given in the
> page 428 of Summerfield's book "Rapid GUI Programming with Python and Qt" is 
> wrong?
> Also I find misleading the Qt documentation regarding this subject
> (right now I can only have a look to the Qt-6 docs). On one side the
> QAbstractItemModel.rowCount documentation agrees with you (or
> vice versa :-). But reading the "Model Subclassing Reference" page,
> section "Read-only access" one can see:

I'm not sure what you're trying to express with the quote, but as I said
the Qt api docs are really lacking in-depth information about the exact
details of the contract that QAbstractItemModel and the View/Delegate
classes expected to be hold. 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.

Anyway, about the columnCount. That one is AFAIK not as problematic
because there's no view that actually supports different amounts of
columns depending on the tree hierarchy. Hence its usually ok to return
a static number from this function.

To find out what exactly goes wrong in your case a small runnable
example is necessary, IIRC your sample code had additional dependencies
on other software and sample data to provide something useful. You
should try with some static data first.

Andreas

-- 
Today is National Existential Ennui Awareness Day.


More information about the PyQt mailing list