Results 1 to 3 of 3

Thread: tableview with abstractmodel

  1. #1
    Join Date
    Oct 2015
    Posts
    45
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default tableview with abstractmodel

    Every time I think I understand how to implement a tableview with a model...


    In this case I have two things:

    when I insert a row in the model I've implemented the insert in the model:

    Qt Code:
    1. def insertRows(self, row, count):
    2. targetindex = self.createIndex(row, 0)
    3. self.beginInsertRows(targetindex, self.rowCount(),self.rowCount()+1)
    4. # copy last row data, except add a new StageState for all StageState columns
    5.  
    6. newrow = self.arraydata[self.rowCount()-1][:] # slice [:] clones the row
    7. # new uuid for this character
    8. newrow[0] = '{}'.format(uuid.uuid4())
    9. self.arraydata.append(newrow)
    10. self.endInsertRows()
    11. return True
    To copy to clipboard, switch view to plain text mode 

    This works and the table IS updated with the new row at the correct place in the table.
    self.arraydata is a list...so I don't get why the new row isn't at the end of the table.
    (probably my limited python-ness )

    Also, to delete a row, I implemented:

    Qt Code:
    1. def removeRows(self, row, count):
    2. targetindex =self.createIndex(row, 0)
    3. self.beginRemoveRows(targetindex, row, row-1)
    4. popped_item = self.arraydata.pop(targetindex.row())
    5. self.endRemoveRows()
    6. return True
    To copy to clipboard, switch view to plain text mode 

    This works fine for any row in the table, except the last row.
    It does delete the last row, but, when I click on any other row after the delete:

    QAccessibleTable::child: Invalid index at: 29 2
    Cannot creat accessible child interface for object: QTableView(0x249a530, name = "table_cast") index: 123

    In this particular run the table was 30 rows long and one was removed. I think that's the 29. I have no idea what index: 123 means...since there was/is only 30 rows...


    Regards,
    Mac

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: tableview with abstractmodel

    In your removeRows() method, what happens if you want to remove row # 0? Where does the model look to find row # "-1" in line 3? If you are only removing 1 row, then I believe you should be using "row", "row" for both arguments to the beginRemoveRows() call, and they should be in increasing row index order if you are removing more than one row. Also doesn't make a lot of sense to use "row" to create an index, and then targetIndex.row() to retrieve it in line 4. It's just "row" number; it doesn't transmute into something special when you use it to create a model index.

    Likewise for insertRows() - I think you should be using "self.rowCount()" for both arguments to the beginInsertRows() method if you are inserting only one row.

    See the description in the docs for QAbstractItemModel::beginInsertRows(). In the example given, they insert -two- rows into the model, so the arguments given are "row 1 index" through "row 2 index", which in the example are 4, 5. If only one row was inserted, it would have been 4, 4.

    In all cases, you have misinterpreted the "parent" (first) argument to the begin...Rows() methods. Table models are flat; there are no parents - every row has an "invalid" parent (index.parent().isValid() returns false). Only tree models will have parents. So for this first argument, you should be passing an invalid model index (via QModelIndex() or the index resulting from createIndex( -1, -1 )).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    drmacro (3rd November 2017)

  4. #3
    Join Date
    Oct 2015
    Posts
    45
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: tableview with abstractmodel

    Quote Originally Posted by d_stranz View Post
    In your removeRows() method, what happens if you want to remove row # 0? Where does the model look to find row # "-1" in line 3? If you are only removing 1 row, then I believe you should be using "row", "row" for both arguments to the beginRemoveRows() call, and they should be in increasing row index order if you are removing more than one row. Also doesn't make a lot of sense to use "row" to create an index, and then targetIndex.row() to retrieve it in line 4. It's just "row" number; it doesn't transmute into something special when you use it to create a model index.

    Likewise for insertRows() - I think you should be using "self.rowCount()" for both arguments to the beginInsertRows() method if you are inserting only one row.

    See the description in the docs for QAbstractItemModel::beginInsertRows(). In the example given, they insert -two- rows into the model, so the arguments given are "row 1 index" through "row 2 index", which in the example are 4, 5. If only one row was inserted, it would have been 4, 4.

    In all cases, you have misinterpreted the "parent" (first) argument to the begin...Rows() methods. Table models are flat; there are no parents - every row has an "invalid" parent (index.parent().isValid() returns false). Only tree models will have parents. So for this first argument, you should be passing an invalid model index (via QModelIndex() or the index resulting from createIndex( -1, -1 )).
    Thank you! I had forgotten about the flat-ness of the table models. And, had indeed misinterpreted the row indexes to be given to begin...Rows() methods.
    The "-" indexes was me being sloppy.
    The conversions of indexes to ModelIndexes was copied code from past attempts where I thought it was telling me an index object was needed not an integer...so, me lost in a fog, again.

    The insert happening in the right places, even though I couldn't see how the insertRows() method could be putting there...is really embarrassing I had circumvented the method all together elsewhere in the code.

Similar Threads

  1. Using tableview with CSV
    By akshaysulakhe in forum Qt Programming
    Replies: 3
    Last Post: 19th July 2013, 10:28
  2. Replies: 1
    Last Post: 25th February 2011, 00:19
  3. tableview
    By GuL in forum Newbie
    Replies: 1
    Last Post: 26th August 2008, 18:18
  4. tableView Problem
    By hrcariaga in forum Newbie
    Replies: 5
    Last Post: 7th February 2008, 09:29
  5. help in tableview
    By bala in forum Qt Programming
    Replies: 3
    Last Post: 12th November 2007, 16:46

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.