Results 1 to 5 of 5

Thread: refresh Qtreeview after removing row

  1. #1
    Join Date
    Jan 2006
    Posts
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Unhappy refresh Qtreeview after removing row

    Hi,
    I use Qt 4.3.3 on Windows and I have started to make the "simple Dom Model" from Qt examples and Demo editable. So I have a QTreeView based on a DomModel who inherits from QAbstractItemModel. The Data is an XML File.
    I try to remove rows. No problem for datas, after deleting the data and resave it to a new XML file, the data corresponding to the rows has vanished. But the View deletes always the last row of the same parent Index, so Model and View are not in the same state anymore! Here is my code:
    Qt Code:
    1. void DomModel::deleteNode(QModelIndex& rCurrentIndex)
    2. {
    3. emit layoutAboutToBeChanged();
    4. beginRemoveRows(rCurrentIndex.parent(), rCurrentIndex.row(), rCurrentIndex.row());
    5. removeRow(rCurrentIndex.row(),rCurrentIndex.parent());
    6. endRemoveRows();
    7. emit layoutChanged();
    8. }
    9. ...
    10. bool DomModel::removeRow(int BeginRow,const QModelIndex& rParentIndex)
    11. {
    12. QModelIndex TypeIndex = rParentIndex.child(BeginRow, TYPE_ARGUMENT);
    13. QModelIndex AttrIndex = rParentIndex.child(BeginRow, ATTRIBUTES_ARGUMENT);
    14. DomItem* pItem = static_cast<DomItem*>(TypeIndex.internalPointer());
    15. QDomNode Node = pItem->node();
    16. QDomNode ParentNode = Node.parentNode();
    17. ParentNode.removeChild(Node);
    18. return true;
    19. }
    To copy to clipboard, switch view to plain text mode 
    Any ideas?
    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: refresh Qtreeview after removing row

    You need to use QAbstractItemModel::beginRemoveRows() and QAbstractItemModel::endRemoveRows()
    BTW. The parent can be an empty model index thus QModelIndex::child() will return an empty model index as well. Instead you should write:
    Qt Code:
    1. QModelIndex childIndex = index(row, column, parent);
    To copy to clipboard, switch view to plain text mode 

    Edit: I just noticed you are using begin/endRemoveRows, I'm not sure if it's in proper context though... What does your QAbstractItemModel::removeRows() look like?

    Edit2: Don't emit those two signals you emit, begin/endRemoveRows will take care of that.
    Last edited by wysota; 12th March 2008 at 20:24.

  3. #3
    Join Date
    Jan 2006
    Posts
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: refresh Qtreeview after removing row

    I have made the change for having the index with child(row,column,parent) to ensure I have a parent.
    Edit: I just noticed you are using begin/endRemoveRows, I'm not sure if it's in proper context though... What does your QAbstractItemModel::removeRows() look like?
    It was a mistake... It is removeRow(), the next method in my code

    Edit2: Don't emit those two signals you emit, begin/endRemoveRows will take care of that.
    Ok, but anyway, same problem...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: refresh Qtreeview after removing row

    Well... removeRow() is not virtual so reimplementing it doesn't make much sense. You should reimplement removeRows(). removeRow() calls removeRows(), so it will work corectly. Then your deleteNode() method will not be needed anymore (it's not needed now too).

    removeRows should look more or less like this:
    Qt Code:
    1. bool DomModel::removeRows(int row,int count, const QModelIndex &parent) {
    2. DomItem* pItem = parent.isValid() ? static_cast<DomItem*>(parent.internalPointer()) : rootItem;
    3. QDomNode Node = pItem->node();
    4. beginRemoveRows(parent, row, row+count-1);
    5. while(count--){
    6. Node.removeChild(pItem->child(row+count)->node());
    7. }
    8. endRemoveRows();
    9. return true;
    10. }
    To copy to clipboard, switch view to plain text mode 

    This will probably not work, becasue DomItem is not prepared for recalculating its children, so you'll have to correct it as well.

  5. #5
    Join Date
    Jan 2006
    Posts
    3
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: refresh Qtreeview after removing row

    Hello,
    I call removeRows() now and reimplement it as you show, thanks. But I think you pointed the real problem, my DomItem is not prepared to recalculate itself. As I understand, I have to "refresh" my model after removing the data from him (a QDomNode). But I can you be more precise about it, please? As I start from the Qt example, I have in my DomItem object a pointer to his parent Item, a QHash of child items (m_pChildItemHash) and a member "rowNumber". As I see in debug, the child(int Index) method is called several times and I guess I mix a little bit data (QDomNode) and Item structure
    Qt Code:
    1. Node.removeChild(pItem->child(row+count)->node());
    To copy to clipboard, switch view to plain text mode 
    that erase the data Node and is ok. I clear now the DomItem from the its ParentItem child's list with:
    Qt Code:
    1. DomItem* pParentItem = pItem->parent();
    2. pParentItem->removeChild(BeginRow);
    3. ...
    4. void DomItem::removeChild(int Index)
    5. {
    6. m_pChildItemHash.remove(Index);
    7. }
    To copy to clipboard, switch view to plain text mode 
    but it is not enough. Where did I miss something?
    Thanks

Similar Threads

  1. Removing "sort" from a QTreeView
    By 3dch in forum Qt Programming
    Replies: 6
    Last Post: 12th August 2006, 16:12
  2. Removing the selected row in a QTreeView
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 29th May 2006, 12:21

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.