Results 1 to 14 of 14

Thread: QTreeView and QAbstractionModel

  1. #1
    Join Date
    Aug 2007
    Posts
    166
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 14 Times in 14 Posts

    Default QTreeView and QAbstractionModel

    Hi all,

    I've managed how to add rows and columns with data in them(its a bit complicated for me) but I can't understand how I can remove rows with all the data in them.
    For example first how I can make the TreeView in that way that I can select multiple items/rows and then with clicking on buton to remove only the selected ones. If someone can give me some example and maybe where I can read more about this modelling and TreeView. I readed a lot in doc.trolltech.com but I still can't understand how all thoose things works.

    Thanks.

  2. The following user says thank you to The Storm for this useful post:

    mhoover (29th August 2009)

  3. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: QTreeView and QAbstractionModel

    Hi, you can adjust QAbstractItemView::selectionMode to make it possible to select multiple items in a view.

    To remove items, you could implement QAbstractItemModel::removeRows() more or less like this:
    Qt Code:
    1. bool MyModel::removeRows(int row, int count, const QModelIndex &parent)
    2. {
    3. // a sanity check
    4. if (row < 0 || count <= 0 || (row + count) > rowCount(parent))
    5. return false;
    6.  
    7. // inform model-view frame work about upcoming removal
    8. beginRemoveRows(QModelIndex(), row, row + count - 1);
    9.  
    10. // remove corresponding items from the internal data structure in here
    11.  
    12. // inform model-view frame work about finished removal
    13. endRemoveRows();
    14.  
    15. // removal succeed
    16. return true;
    17. }
    To copy to clipboard, switch view to plain text mode 
    Now removing a bunch of selected items is kind of problematic. You cannot just remove them in an arbitrary order from the model because removing a certain item might cause other indices to become invalid. The solution is to remove items in reverse order, from end to beginning.
    J-P Nurmi

  4. #3
    Join Date
    Aug 2007
    Posts
    166
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 14 Times in 14 Posts

    Default Re: QTreeView and QAbstractionModel

    Is it needed to make new class, can I just use the Standart model for the construction?

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: QTreeView and QAbstractionModel

    Wait, which model are you using? I thought you already had QAbstractItemModel subclass.
    J-P Nurmi

  6. #5
    Join Date
    Aug 2007
    Posts
    166
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 14 Times in 14 Posts

    Default Re: QTreeView and QAbstractionModel

    QAbstractItemModel *pModel = new QStandardItemModel(parent);
    This is what I use, I saw it in one example.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QTreeView and QAbstractionModel

    So you should be able to retrieve selected rows through QItemSelectionModel of the tree view and remove them from the model.

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: QTreeView and QAbstractionModel

    Oh, you should've mentioned that you're using QStandardItemModel. So forget about reimplementing removeRows() then, QStandardItemModel already implements it. Just call it.
    J-P Nurmi

  9. The following user says thank you to jpn for this useful post:

    mhoover (29th August 2009)

  10. #8
    Join Date
    Aug 2007
    Posts
    166
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 14 Times in 14 Posts

    Default Re: QTreeView and QAbstractionModel

    Can you show me example how can I remove the selected rows?

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QTreeView and QAbstractionModel

    Call removeRow() on the contents of QItemSelectionModel of the view.

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

    mhoover (29th August 2009)

  13. #10
    Join Date
    Aug 2007
    Posts
    166
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 14 Times in 14 Posts

    Default Re: QTreeView and QAbstractionModel

    QTreeView doesn't have member that return QItemSelectionModel.

  14. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: QTreeView and QAbstractionModel

    J-P Nurmi

  15. #12
    Join Date
    Aug 2007
    Posts
    166
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 14 Times in 14 Posts

    Default Re: QTreeView and QAbstractionModel

    Ok is it bug in Qt 4.3.1 on Windows that when I remove row it is removed but its like the others become hidden and when I add new they show up again?

  16. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QTreeView and QAbstractionModel

    Not likely. Can we see the code you wrote?

  17. #14
    Join Date
    Aug 2007
    Posts
    166
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 14 Times in 14 Posts

    Default Re: QTreeView and QAbstractionModel

    Oh, it was my mistake, I though that I must set the model each time when I change it, because I though that setModel() just copy the pointer but it seems its not like that, now all works fine, thanks a lot.

  18. The following user says thank you to The Storm for this useful post:

    mhoover (29th August 2009)

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
  •  
Qt is a trademark of The Qt Company.