Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How does row numbering in a QStandardItemModel work
Qt 6.12 LTS is out! See what's new in the release blog

How does row numbering in a QStandardItemModel work

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    voidtrance
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What do you mean by the second list is empty ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      V 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What do you mean by the second list is empty ?

        V Offline
        V Offline
        voidtrance
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What do you have if you use something like:

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt
          • Unsolved