[PyQt] PyQt Digest, Vol 137, Issue 8

David Cortesi davecortesi at gmail.com
Sun Dec 6 19:34:23 GMT 2015


> Date: Sun, 6 Dec 2015 02:20:10 +0100
> From: <c.buhtz at posteo.jp>
> Subject: [PyQt] how to insertRow() in a QAbstractListModel?
>
> I have a QListView and created a QAbstractListModel-derived beside it.
> The model object just have a python string-list as data.
>
> As the docu tells me I tried to implement insertRows() and
> removeRows(). But I don't understand the logic behind it.
> <http://doc.qt.io/qt-5/qabstractitemmodel.html#insertRows>
>
> The signature (arguments/parameters) of the method doesn't have data in.
> Where is the data to be inserted?
>
>
Agree, this is not clear in the documentation.

A better overview is at
http://doc.qt.io/qt-5/model-view-programming.html#model-classes (search on
"insert").

What none of this says clearly is that insertRow() and insertRows() only
add EMPTY rows to the model. You can see this (by implication) in the
example in the above-linked page. It merely adds empty strings to a string
list.

When will you populate the new empty rows with data? I am not sure. One
possible answer is, when the View needs to display the data, it calls your
data() method. That method could say, oh, there is no data in this row and
column, I had better fetch some. How does your data() now locate the data
for a given row and column? All it knows is the row and column numbers.

Be sure to read the discussion on beginInsertRows() (
http://doc.qt.io/qt-5/qabstractitemmodel.html#beginInsertRows) and
endInsertRows.

What will happen is, when you call endInsertRows(), almost immediately the
View will figure out if any of the inserted rows are visible in the window,
and it will call the model data() to get display values for the visible
cells.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20151206/b601c2fa/attachment.html>


More information about the PyQt mailing list