Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How does row numbering in a QStandardItemModel work



  • I am having some difficulty understanding how row numbering in QStandardItemModel() works. For example: I have a model which has two rows of 5 columns each. I want to remove the rows from the model without losing the actual data. Let's say that the row indexes are 0 and 1.

    I am using PyQt4 so I can't paste C++ code here but the code does the following:

    for index in 1, 0:
        rowData = model->takeRow(index)
    

    The reason for the inverted sequence is that, as far as I understand it, when a row is removed with takeRow(), the remaining rows are re-numbered. So the logic here is, take rows away from the end.

    However, the above only results in a single list of QStandradItems(). The second list is empty. Can someone explain this to me, please?


  • Lifetime Qt Champion

    Hi,

    What do you mean by the second list is empty ?



  • @SGaist said:

    Hi,

    What do you mean by the second list is empty ?

    takeRow() returns a QList (in PyQt4, it is just a standard Python list) of QStandardItems(). If you execute the loop for 2 iterations, you should get two lists (one for each row). On the first iteration, I get the items from the second row. However, on the second iteration, the return value is an empty list.


  • Lifetime Qt Champion

    What do you have if you use something like:

    while model.rowCount():
        rowData = model.takeRow(mode.rowCount() -1)
        print rowData
    

Log in to reply