Results 1 to 3 of 3

Thread: problem with custom QAbstractItemModel derived class when changing data

  1. #1
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default problem with custom QAbstractItemModel derived class when changing data

    Hi!

    I have a problem with my class derived from QAbstractItemModel when I want to delete an item.

    I already tried signals layoutChanged() and beginRemoveRows()/endRemoveRows() and have no idea, what can be wrong.

    I attached a simple example of my problem.
    As you run it, please select the third row. After a few seconds I'm removing the second item and sometimes it causes segfault. The backtrace from GDB looks like this:

    Qt Code:
    1. (gdb) backtrace
    2. #0 0x0804e667 in AbstractModel::parent(QModelIndex const&) const ()
    3. #1 0x0086660c in ?? () from /usr/lib/libQtGui.so.4
    4. #2 0x00866dcb in ?? () from /usr/lib/libQtGui.so.4
    5. #3 0x00867111 in QItemSelectionModel::qt_metacall(QMetaObject::Call, int, void**) () from /usr/lib/libQtGui.so.4
    6. #4 0x00d17c9a in QMetaObject::metacall(QObject*, QMetaObject::Call, int, void**) () from /usr/lib/libQtCore.so.4
    7. #5 0x00d263d5 in QMetaObject::activate(QObject*, QMetaObject const*, int, void**) () from /usr/lib/libQtCore.so.4
    8. #6 0x00d76d77 in QAbstractItemModel::layoutChanged() () from /usr/lib/libQtCore.so.4
    9. #7 0x0805529b in AbstractModel::qt_metacall(QMetaObject::Call, int, void**) ()
    10. #8 0x00d17c9a in QMetaObject::metacall(QObject*, QMetaObject::Call, int, void**) () from /usr/lib/libQtCore.so.4
    11. #9 0x00d263d5 in QMetaObject::activate(QObject*, QMetaObject const*, int, void**) () from /usr/lib/libQtCore.so.4
    12. #10 0x08054ff9 in AbstractItem::updateModel() ()
    13. #11 0x0804d335 in AbstractItem::operator-=(AbstractItem*) ()
    14. #12 0x0804ddc8 in AbstractItem::update() ()
    15. #13 0x0805505d in AbstractItem::qt_metacall(QMetaObject::Call, int, void**) ()
    16. #14 0x00d17c9a in QMetaObject::metacall(QObject*, QMetaObject::Call, int, void**) () from /usr/lib/libQtCore.so.4
    17. #15 0x00d263d5 in QMetaObject::activate(QObject*, QMetaObject const*, int, void**) () from /usr/lib/libQtCore.so.4
    18. #16 0x00d77aa7 in QTimer::timeout() () from /usr/lib/libQtCore.so.4
    To copy to clipboard, switch view to plain text mode 

    My question is: How can I safely delete data and inform the model (and treeview) about this change?
    Attached Files Attached Files
    Last edited by slodki; 12th May 2010 at 09:47.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: problem with custom QAbstractItemModel derived class when changing data

    Hi,

    it is a simple "out of range" problem in AbstractModel::index() line 133. Write
    Qt Code:
    1. if ( (row >= 0) && parentObject->children().count() > row ) {
    To copy to clipboard, switch view to plain text mode 
    instead. Then you just have to make sure your selection is updated.

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

    slodki (12th May 2010)

  4. #3
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with custom QAbstractItemModel derived class when changing data

    Thanks for a quick answer. This line was obviously incorrect, but it does not solve all the issues.

    If you try a random combination of double clicking or holding mouse button on the item that is going to be removed there is still an issue with parent() function.

    The backtrace from GDB gives something like:

    Qt Code:
    1. (gdb) back
    2. #0 0x0804e73d in AbstractModel::parent(QModelIndex const&) const ()
    3. #1 0x008478a6 in QTreeView::drawRow(QPainter*, QStyleOptionViewItem const&, QModelIndex const&) const () from /usr/lib/libQtGui.so.4
    4. #2 0x008514a9 in QTreeView::drawTree(QPainter*, QRegion const&) const () from /usr/lib/libQtGui.so.4
    5. #3 0x008545bd in QTreeView::paintEvent(QPaintEvent*) () from /usr/lib/libQtGui.so.4
    6. #4 0x002c16b6 in QWidget::event(QEvent*) () from /usr/lib/libQtGui.so.4
    7. #5 0x006bdfd3 in QFrame::event(QEvent*) () from /usr/lib/libQtGui.so.4
    8. #6 0x00758382 in QAbstractScrollArea::viewportEvent(QEvent*) () from /usr/lib/libQtGui.so.4
    9. #7 0x0080cbd7 in QAbstractItemView::viewportEvent(QEvent*) () from /usr/lib/libQtGui.so.4
    10. #8 0x0084df7c in QTreeView::viewportEvent(QEvent*) () from /usr/lib/libQtGui.so.4
    11. #9 0x0075ac65 in ?? () from /usr/lib/libQtGui.so.4
    12. #10 0x00d11cda in QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject*, QEvent*) () from /usr/lib/libQtCore.so.4
    13. #11 0x002634b9 in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/libQtGui.so.4
    14. #12 0x0026a0f9 in QApplication::notify(QObject*, QEvent*) () from /usr/lib/libQtGui.so.4
    15. #13 0x00d12a3b in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib/libQtCore.so.4
    16. #14 0x002ca9d6 in QWidgetPrivate::drawWidget(QPaintDevice*, QRegion const&, QPoint const&, int, QPainter*, QWidgetBackingStore*) () from /usr/lib/libQtGui.so.4
    17. #15 0x004a17d7 in ?? () from /usr/lib/libQtGui.so.4
    18. #16 0x002bab93 in QWidgetPrivate::syncBackingStore() () from /usr/lib/libQtGui.so.4
    19. #17 0x002c1e76 in QWidget::event(QEvent*) () from /usr/lib/libQtGui.so.4
    20. #18 0x006bdfd3 in QFrame::event(QEvent*) () from /usr/lib/libQtGui.so.4
    21. #19 0x00758d97 in QAbstractScrollArea::event(QEvent*) () from /usr/lib/libQtGui.so.4
    22. #20 0x0080582c in QAbstractItemView::event(QEvent*) () from /usr/lib/libQtGui.so.4
    23. #21 0x002634dc in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/libQtGui.so.4
    24. #22 0x0026a0f9 in QApplication::notify(QObject*, QEvent*) () from /usr/lib/libQtGui.so.4
    25. #23 0x00d12a3b in QCoreApplication::notifyInternal(QObject*, QEvent*) () from /usr/lib/libQtCore.so.4
    26. #24 0x00d15473 in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) () from /usr/lib/libQtCore.so.4
    27. #25 0x00d155dd in QCoreApplication::sendPostedEvents(QObject*, int) () from /usr/lib/libQtCore.so.4
    To copy to clipboard, switch view to plain text mode 

    I should mention that this of course is a simple code for bug catching taken from a bigger application with enhanced functionality.

Similar Threads

  1. Can not cast into the derived class from QScrollBar
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2008, 10:23
  2. no frame on Qlabel derived class
    By tupu_83 in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2008, 16:01
  3. Can't compile custom class derived from QGLWidget
    By stealth86 in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2007, 17:55
  4. rtti() of derived class
    By quickNitin in forum Newbie
    Replies: 4
    Last Post: 8th October 2006, 14:20
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

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.