Results 1 to 3 of 3

Thread: Removing the selected row in a QTreeView

  1. #1
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Removing the selected row in a QTreeView

    I'm using a QTreeView and a custom model to display a tree of MIME types by group. The top-level items are the MIME groups (application, image, text,...) and each one has the list of MIME types belonging to that group (application/octet-stream, text/plain,...).

    I get the selected MIME type and clear the selection, then I remove it from my internal data structures and I want to remove it also from the model. I'm using this method:

    Qt Code:
    1. void MIMETypesModel::removeMimeType(QString mimeType)
    2. {
    3. QString group = mimeType.left(mimeType.indexOf('/'));
    4. MIMETypesItem *groupItem = m_rootItem->findChild(group);
    5. if(!groupItem)
    6. return;
    7.  
    8. MIMETypesItem *mimeItem = groupItem->findChild(mimeType);
    9. if(mimeItem != NULL) {
    10. QModelIndex parentIndex = createIndex(groupItem->row(), 0, m_rootItem);
    11. beginRemoveRows(parentIndex, mimeItem->row(), mimeItem->row());
    12. groupItem->removeChild(mimeItem);
    13. endRemoveRows();
    14. }
    15.  
    16. if(groupItem->childCount() == 0) {
    17. beginRemoveRows(QModelIndex(), groupItem->row(), groupItem->row());
    18. m_rootItem->removeChild(groupItem);
    19. endRemoveRows();
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    MIMETypesItem is based in the "Simple Tree Model" example from QT. MIMETypesItem::findChild() returns the child item that contains the specified MIME type/group (it searches for the string in the child items).

    It's really strange, but if I remove, let's say, the second MIME type in the first group, it works flawlessly. But if I remove, let's say, the tenth MIME type in the first group, the program crashes in the beginRemoveRows() with a SIGABRT and this is printed to stdout:

    ASSERT failure in QList<T>::at: "index out of range", file /usr/lib/qt4/include/QtCore/qlist.h, line 361
    This is the backtrace:

    Qt Code:
    1. (gdb) bt
    2. #0 0xffffe410 in __kernel_vsyscall ()
    3. #1 0xb72a37d0 in raise () from /lib/libc.so.6
    4. #2 0xb72a4ea3 in abort () from /lib/libc.so.6
    5. #3 0xb75044ec in qt_message_output (msgType=QtFatalMsg,
    6. buf=0xbf99f4d0 "ASSERT failure in QList<T>::at: \"index out of range\", file /usr/lib/qt4/include/QtCore/qlist.h, line 361")
    7. at global/qglobal.cpp:1958
    8. #4 0xb750453e in qFatal (msg=0xb75e9e8c "ASSERT failure in %s: \"%s\", file %s, line %d") at global/qglobal.cpp:2174
    9. #5 0xb7504574 in qt_assert_x (where=0x80960a3 "QList<T>::at", what=0x8096090 "index out of range",
    10. file=0x809606c "/usr/lib/qt4/include/QtCore/qlist.h", line=361) at global/qglobal.cpp:1731
    11. #6 0x080902cc in QList<MIMETypesItem*>::at (this=0x82cb2bc, i=11) at /usr/lib/qt4/include/QtCore/qlist.h:361
    12. #7 0x0808f8b9 in MIMETypesItem::child (this=0x82cb2b8, row=11) at mimetypesitem.cpp:80
    13. #8 0x0808ee78 in MIMETypesModel::index (this=0x82cacf0, row=11, column=0, parent=@0xbf9a1bec) at mimetypesmodel.cpp:104
    14. #9 0xb7d2ceec in QTreeView::rowsAboutToBeRemoved (this=0x82c0dd8, parent=@0xbf9a1bec, start=11, end=11) at itemviews/qtreeview.cpp:1341
    15. #10 0xb7cfd46e in QAbstractItemView::qt_metacall (this=0x82c0dd8, _c=QMetaObject::InvokeMetaMethod, _id=17, _a=0xbf9a1b54)
    16. at .moc/debug-shared/moc_qabstractitemview.cpp:176
    17. #11 0xb7d8071e in QTreeView::qt_metacall (this=0x82c0dd8, _c=QMetaObject::InvokeMetaMethod, _id=43, _a=0xbf9a1b54)
    18. at .moc/debug-shared/moc_qtreeview.cpp:88
    19. #12 0xb75c7614 in QMetaObject::activate (sender=0x82cacf0, from_signal_index=9, to_signal_index=9, argv=0xbf9a1b54)
    20. at kernel/qobject.cpp:2671
    21. #13 0xb75c845f in QMetaObject::activate (sender=0x82cacf0, m=0x80b9a58, local_signal_index=5, argv=0xbf9a1b54) at kernel/qobject.cpp:2709
    22. #14 0xb75e8a13 in QAbstractItemModel::rowsAboutToBeRemoved (this=0x82cacf0, _t1=@0xbf9a1bec, _t2=11, _t3=11)
    23. at .moc/debug-shared/moc_qabstractitemmodel.cpp:149
    24. #15 0xb75ac044 in QAbstractItemModel::beginRemoveRows (this=0x82cacf0, parent=@0xbf9a1bec, first=11, last=11)
    25. at kernel/qabstractitemmodel.cpp:1727
    26. #16 0x0808e519 in MIMETypesModel::removeMimeType (this=0x82cacf0, mimeType=@0xbf9a1c6c) at mimetypesmodel.cpp:215
    27. #17 0x080870d3 in MIMETypesEditor::removeMimeType (this=0x8255b90, type=@0xbf9a1ca0) at mimetypeseditor.cpp:291
    28. #18 0x0808ab1a in MIMETypesPanel::removeMimeType (this=0x82bab58) at mimetypespanel.cpp:172
    29. #19 0x08092294 in MIMETypesPanel::qt_metacall (this=0x82bab58, _c=QMetaObject::InvokeMetaMethod, _id=0, _a=0xbf9a21ec)
    30. at moc_mimetypespanel.cpp:67
    31. #20 0xb75c7614 in QMetaObject::activate (sender=0x82c4078, from_signal_index=25, to_signal_index=26, argv=0xbf9a21ec)
    32. at kernel/qobject.cpp:2671
    33. #21 0xb75c7801 in QMetaObject::activate (sender=0x82c4078, m=0xb7ea3690, from_local_signal_index=2, to_local_signal_index=3, argv=0xbf9a21ec)
    34. at kernel/qobject.cpp:2718
    35. #22 0xb7d79148 in QAbstractButton::clicked (this=0x82c4078, _t1=false) at .moc/debug-shared/moc_qabstractbutton.cpp:170
    To copy to clipboard, switch view to plain text mode 

    Any idea?

  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: Removing the selected row in a QTreeView

    If I understand correctly, you are doing it the wrong way.

    You should reimplement QAbstractItemModel::removeRow() so that it removes an item from your internal data structures. This way calling removeRow (or removeRows, as it uses removeRow internally) will update the model and inform all views about it. If you operate on internal structures using your own methods, you don't have to use beginRemoveRows() (and the "index" approach in general) as the data is already removed from the model -- just remove internal structures associated with items and emit layoutChanged() from the model, so that views can update themselves.

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

    TheRonin (22nd March 2012)

  4. #3
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Removing the selected row in a QTreeView

    It worked, thanks!

Similar Threads

  1. Highlighting selected section on QDateTimeEdit
    By Yorma in forum Qt Programming
    Replies: 4
    Last Post: 3rd May 2013, 12:03
  2. QTreeView repaint
    By graeme in forum Qt Programming
    Replies: 17
    Last Post: 13th March 2012, 13:27
  3. Zooming a paricular selected region
    By Kapil in forum Newbie
    Replies: 8
    Last Post: 9th May 2006, 14:41
  4. paint QTreeView item !!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2006, 14:24
  5. QSortFilterProxyModel & QTreeView
    By Bear in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 15:04

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.