Elaborate example of a custom widget implementing MVC

Maurizio Berti maurizio.berti at gmail.com
Tue Jun 22 17:51:47 BST 2021


I cannot suggest actual resources, as I learned the Qt Model/View the hard
way: trying and experimenting (sometimes "randomly"), studying the
documentation and the sources.

But, in the meantime, if you give us more precise questions, we might be
able to help.

For instance, if you're referring to the argument of rowCount and index,
the QModelIndex indicates an "empty" (and invalid) index that's commonly
considered or referred to as the "root" index of the model.

A model can have more than two dimensions (tree models), and each index
could be a child or parent of another, so the actual coordinates of an
index are always three: row, column, parent ("depth").
When using table models, there obviously is no parent (since it's a
bidimensional model), and that can be traced by having an invalid index (a
new QModelIndex instance).
This is fundamental as the model has to know to what refer to when you only
have one or two dimensions:

rowCount(row, parent): returns the row count of the *parent*, if the parent
is invalid, it's assumed that it refers to the row count of the "top level".
index(row, column, parent): returns a QModelIndex at the given row/column
*referred* to the parent; again, if the parent is invalid, it's the "top
level" index at that row and column.

Note that it's common to default the parent argument to None for simple
models, but it's usually better (especially for tree models) to use a
QModelIndex() instead.

Hope this helps,

Maurizio

Il giorno mar 22 giu 2021 alle ore 10:32 Stefan de Konink <stefan at konink.de>
ha scritto:

> Hello,
>
> I have implemented a custom widget presenting a swimming lane graph that
> can interact with a model. It works pretty well, but there are some basic
> things I don't understand, for example the reason for adding
> QtCore.QModelIndex() to things like rowCount, and index.
>
> Is there any good tutorial or are there any reference widgets written in
> Python that are considered best in class and good learning material?
>
> --
> Stefan
>


-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210622/096d2414/attachment.htm>


More information about the PyQt mailing list