[PyQt] About dimensions of QAbstractTableModel

Vicent Mas uvemas at gmail.com
Mon Nov 29 22:03:10 GMT 2010


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:
> > > 
> > > [...]
> > > 
> > >>     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. 

I completely agree.

> 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.

> 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.
> 

I've no sent any sample code (I'm not Ian but Vicent :-). I'm just reading the 
mailing list and trying to improve my understanding of models and views.

Vicent
::

	Share what you know, learn what you don't

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20101129/f19405f4/attachment.pgp>


More information about the PyQt mailing list