1 Attachment(s)
QAbstractItemModel segmentation fault in QTreeView
Hello everyone, I have following code and its connected to treeview that does load item correctly
Code:
#include "listmodel.h"
ListModel
::ListModel(QObject *parent
) :{
_data=new QList<MyClass*>();
_data->append(new MyClass("first item"));
_data->append(new MyClass("second item"));
}
int ListModel
::rowCount(const QModelIndex &parent
) const {
return _data->count();
}
int ListModel
::columnCount(const QModelIndex &parent
) const {
return 3;
}
{
}
bool ListModel
::insertRows(int position,
int row,MyClass cls,
const QModelIndex &parent
){ beginInsertRows(parent,position,position+row-1);
for(int r=0;r<row;r++)
{
this->_data->insert(position,&cls);
}
endInsertRows();
return true;
}
{
if (column < 0 || column >= 4 || row < 0 || parent.column() > 0)
return createIndex(row, column, 0);
}
QVariant ListModel
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
if (role != Qt::DisplayRole)
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("FileName");
case 1:
return tr("Dest");
case 2:
return tr("Dest Name");
default:
}
}
}
{
//if (index.row() < 0 || index.row() >=3)
// return QVariant();
switch( role )
{
case Qt::DisplayRole:
{
if (index.column() == 0)
return _data->at(index.row())->firstCol;
else if (index.column() == 1)
return _data->at(index.row())->secondCol;
else if (index.column() == 2)
return _data->at(index.row())->thirdCol;
}
case Qt::EditRole:
{
if (index.column() == 0)
return _data->at(index.row())->firstCol;
else if (index.column() == 1)
return _data->at(index.row())->secondCol;
else if (index.column() == 2)
return _data->at(index.row())->thirdCol;
}
default:
}
}
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
Re: QAbstractItemModel segmentation fault in QTreeView
All the Model-View Interface Implmentations should check for QModelIndex ::isValid() condition and handle it accordingly (safely)
Example:
Code:
int ListModel
::rowCount(const QModelIndex &parent
) const {
if(parent.isValid())
return _data->count(); //root node
else
return 0;
}