Results 1 to 6 of 6

Thread: ListModel.remove() crashes program

  1. #1
    Join Date
    Nov 2008
    Posts
    16
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post ListModel.remove() crashes program

    I've managed to reproduce the crash in the following code. The version of Qt i'm using is 4.7.4. Can anyone tell me why this would crash the program? Does it crash for anyone else?

    Qt Code:
    1. import QtQuick 1.0
    2.  
    3. Rectangle {
    4. id: main
    5. width: 360
    6. height: 360
    7.  
    8. Text {
    9. id: addBtn
    10. text: "Add to List"
    11. anchors.top: parent.top
    12. width: parent.width
    13. height: 40
    14. horizontalAlignment: Text.AlignHCenter
    15. verticalAlignment: Text.AlignVCenter
    16. font.bold: true
    17.  
    18. MouseArea {
    19. anchors.fill: parent
    20. onClicked: {
    21. list_model.append({"display": "Click me to delete me"})
    22. }
    23. }
    24. }
    25.  
    26. ListView {
    27. anchors.top: parent.top
    28. anchors.topMargin: 40
    29. anchors.left: parent.left
    30. anchors.right: parent.right
    31. anchors.bottom: parent.bottom
    32. spacing: 5
    33.  
    34. model: ListModel {
    35. id: list_model
    36. }
    37.  
    38. delegate: Rectangle
    39. {
    40. border.width: 1
    41. border.color: "#000"
    42. width: main.width - 1
    43. height: delText.height + 8
    44.  
    45. MouseArea {
    46. anchors.fill: parent
    47. onClicked: {
    48. list_model.remove(index)
    49. }
    50. }
    51.  
    52. Text {
    53. id: delText
    54. height: 20
    55. anchors.horizontalCenter: parent.horizontalCenter
    56. anchors.verticalCenter: parent.verticalCenter
    57. text: display
    58. }
    59. }
    60. }
    61. }
    To copy to clipboard, switch view to plain text mode 

  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: ListModel.remove() crashes program

    What should index be? Try .remove(currentIndex)!

  3. #3
    Join Date
    Nov 2008
    Posts
    16
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: ListModel.remove() crashes program

    Quote Originally Posted by Lykurg View Post
    What should index be? Try .remove(currentIndex)!
    index is whatever the index of the item is that has been interacted with or interacted with one of its children. For my current project, which isn't entirely reflected in the reproducable code, in order to remove the item from the list you have to interact with a child element of the item. This means currentIndex is not practical for my current project, since interacting with the child element does not necessarily make the item parent the current item. Nevertheless, if you were to replace index with ListView.currentIndex (or whatever gives you the current index) in the reproducable code, the program would still crash.

  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: ListModel.remove() crashes program

    Removing the current item inside any routine that handles that item is not safe. Mark the item for deletion and delete it after the control returns to the event loop. You might need to interface with C++ for that.
    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.


  5. #5
    Join Date
    Nov 2008
    Posts
    16
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: ListModel.remove() crashes program

    Quote Originally Posted by wysota View Post
    Removing the current item inside any routine that handles that item is not safe. Mark the item for deletion and delete it after the control returns to the event loop. You might need to interface with C++ for that.
    Researching what you said, i found this example in the documentation which also calls model_id.remove(index) from the delegate: http://doc.qt.nokia.com/4.7-snapshot...clist-qml.html Skimming through the example, i don't really see any fundamental difference between my code (or the crash reproducable code) and the example.

  6. #6
    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: ListModel.remove() crashes program

    The fundamental difference is in ListView.delayRemove=true and everything around it.
    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.


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

    truefusion (6th February 2012)

Similar Threads

  1. Program crashes on OS X Lion
    By fyodor in forum Newbie
    Replies: 1
    Last Post: 31st October 2011, 06:18
  2. Program crashes
    By Fallen_ in forum Qt Programming
    Replies: 49
    Last Post: 20th September 2010, 01:41
  3. Program crashes while trying to do a check in constructor
    By hakermania in forum Qt Programming
    Replies: 5
    Last Post: 1st September 2010, 10:15
  4. program crashes (QtTestRunner)
    By fmariusd in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2008, 09:27
  5. Program crashes (SIGSEGV)
    By Voldemort in forum Qt Programming
    Replies: 47
    Last Post: 21st May 2007, 20:09

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.