Results 1 to 3 of 3

Thread: removeRows in a QTreeView

  1. #1
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default removeRows in a QTreeView

    I am using the basic QStandardItemModel to populate a QTreeView.

    I have looked over all the removeRow/removeRows issues I could find on this forum, but they were not able to solve my problem.

    I have been retrieving indexes to append and edit items in the QTreeView, but for some reason when I call removeRow on the this index I get bizarre behavior. I typically see nothing happen but occasionally a child node will disappear.

    I have tried removing the nodes using this approach:

    Qt Code:
    1. QModelIndex index = ui.settings_treeView->currentIndex();
    2. QStandardItem *item = settings_model->itemFromIndex( index );
    3. int row = settings_model->itemFromIndex( index )=>row();
    4. settings_model->removeRow( row, index );
    To copy to clipboard, switch view to plain text mode 

    And I have tried this approach:

    Qt Code:
    1. QItemSelectionModel *selection = ui.settings_treeView->selectionModel();
    2. QModelIndex index = ui.settings_treeView->currentIndex();
    3. settings_model->removeRows( selection->currentIndex().row(), 1, index );
    To copy to clipboard, switch view to plain text mode 

    But random nodes start disappearing.

    This seems like something that should be easy. I'm not sure why it isn't working for me.

  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: removeRows in a QTreeView

    You want to remove the currently highlighted item?

    Qt Code:
    1. QTreeView *tv = ...;
    2. QModelIndex current = tv->currentIndex();
    3. if(current.isValid()){
    4. tv->model()->removeRow(current.row(), current.parent());
    5. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    mhoover (1st September 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: removeRows in a QTreeView

    Thanks.

    I wasn't sure about that parent thing. It didn't look easy to find the parent, but I guess it comes with the index.

    Also the validity check is nice to know about.

Similar Threads

  1. QTreeView repaint
    By graeme in forum Qt Programming
    Replies: 17
    Last Post: 13th March 2012, 13:27
  2. Direct root childs inline QTreeView
    By faraslacks in forum Qt Programming
    Replies: 2
    Last Post: 15th January 2009, 07:45
  3. QTreeView help
    By bepaald in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 21:22
  4. background image in QTreeView
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 06:25
  5. paint QTreeView item !!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2006, 14:24

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.