naturalpsychic
25th May 2011, 05:24
Hello everyone, I have following code and its connected to treeview that does load item correctly
#include "listmodel.h"
ListModel::ListModel(QObject *parent) :
QAbstractItemModel(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;
}
QModelIndex ListModel::parent(const QModelIndex &child) const
{
return QModelIndex();
}
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;
}
QModelIndex ListModel::index(int row, int column, const QModelIndex &parent) const
{
if (column < 0 || column >= 4 || row < 0 || parent.column() > 0)
return QModelIndex();
return createIndex(row, column, 0);
}
QVariant ListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("FileName");
case 1:
return tr("Dest");
case 2:
return tr("Dest Name");
default:
return QVariant();
}
}
return QVariant();
}
QVariant ListModel::data(const QModelIndex &index, int role) const
{
//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:
return QVariant();
}
}
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
#include "listmodel.h"
ListModel::ListModel(QObject *parent) :
QAbstractItemModel(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;
}
QModelIndex ListModel::parent(const QModelIndex &child) const
{
return QModelIndex();
}
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;
}
QModelIndex ListModel::index(int row, int column, const QModelIndex &parent) const
{
if (column < 0 || column >= 4 || row < 0 || parent.column() > 0)
return QModelIndex();
return createIndex(row, column, 0);
}
QVariant ListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("FileName");
case 1:
return tr("Dest");
case 2:
return tr("Dest Name");
default:
return QVariant();
}
}
return QVariant();
}
QVariant ListModel::data(const QModelIndex &index, int role) const
{
//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:
return QVariant();
}
}
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