Results 1 to 2 of 2

Thread: QAbstractItemModel segmentation fault in QTreeView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default QAbstractItemModel segmentation fault in QTreeView

    Hello everyone, I have following code and its connected to treeview that does load item correctly

    Qt Code:
    1. #include "listmodel.h"
    2.  
    3. ListModel::ListModel(QObject *parent) :
    4. {
    5. _data=new QList<MyClass*>();
    6.  
    7. _data->append(new MyClass("first item"));
    8. _data->append(new MyClass("second item"));
    9.  
    10.  
    11. }
    12. int ListModel::rowCount(const QModelIndex &parent) const
    13. {
    14. return _data->count();
    15.  
    16. }
    17. int ListModel::columnCount(const QModelIndex &parent) const
    18. {
    19. return 3;
    20.  
    21. }
    22. QModelIndex ListModel::parent(const QModelIndex &child) const
    23. {
    24. return QModelIndex();
    25. }
    26. bool ListModel::insertRows(int position,int row,MyClass cls, const QModelIndex &parent){
    27. beginInsertRows(parent,position,position+row-1);
    28. for(int r=0;r<row;r++)
    29. {
    30.  
    31. this->_data->insert(position,&cls);
    32. }
    33. endInsertRows();
    34. return true;
    35.  
    36. }
    37. QModelIndex ListModel::index(int row, int column, const QModelIndex &parent) const
    38. {
    39. if (column < 0 || column >= 4 || row < 0 || parent.column() > 0)
    40. return QModelIndex();
    41.  
    42.  
    43. return createIndex(row, column, 0);
    44. }
    45.  
    46. QVariant ListModel::headerData(int section, Qt::Orientation orientation, int role) const
    47. {
    48. if (role != Qt::DisplayRole)
    49. return QVariant();
    50.  
    51. if (orientation == Qt::Horizontal) {
    52. switch (section) {
    53. case 0:
    54. return tr("FileName");
    55.  
    56. case 1:
    57. return tr("Dest");
    58.  
    59. case 2:
    60. return tr("Dest Name");
    61.  
    62. default:
    63. return QVariant();
    64. }
    65. }
    66. return QVariant();
    67. }
    68. QVariant ListModel::data(const QModelIndex &index, int role) const
    69. {
    70. //if (index.row() < 0 || index.row() >=3)
    71. // return QVariant();
    72.  
    73. switch( role )
    74. {
    75. case Qt::DisplayRole:
    76. {
    77. if (index.column() == 0)
    78. return _data->at(index.row())->firstCol;
    79. else if (index.column() == 1)
    80. return _data->at(index.row())->secondCol;
    81. else if (index.column() == 2)
    82. return _data->at(index.row())->thirdCol;
    83. }
    84. case Qt::EditRole:
    85. {
    86.  
    87. if (index.column() == 0)
    88. return _data->at(index.row())->firstCol;
    89. else if (index.column() == 1)
    90. return _data->at(index.row())->secondCol;
    91. else if (index.column() == 2)
    92. return _data->at(index.row())->thirdCol;
    93.  
    94. }
    95. default:
    96. return QVariant();
    97. }
    98. }
    To copy to clipboard, switch view to plain text mode 


    But the problem is when i click ">" in treeview, it gives me segmentatioin fault error, while i am not even expecting it to have any children.

    Please have a look at screenshot for display
    Attached Images Attached Images
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QAbstractItemModel segmentation fault in QTreeView

    All the Model-View Interface Implmentations should check for QModelIndex ::isValid() condition and handle it accordingly (safely)

    Example:
    Qt Code:
    1. int ListModel::rowCount(const QModelIndex &parent) const
    2. {
    3. if(parent.isValid())
    4. return _data->count(); //root node
    5. else
    6. return 0;
    7. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Segmentation Fault
    By Ryan Riffle in forum Qt Programming
    Replies: 4
    Last Post: 16th January 2011, 20:52
  2. Segmentation Fault
    By freekill in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2010, 15:31
  3. segmentation fault
    By navid in forum Qt Programming
    Replies: 3
    Last Post: 20th December 2009, 11:40
  4. segmentation fault
    By uchennaanyanwu in forum Newbie
    Replies: 3
    Last Post: 31st July 2008, 16:52
  5. segmentation fault
    By shamik in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2006, 07:33

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.