PDA

View Full Version : Problem with QAbstractListModel



chandan
12th April 2010, 00:24
Hi,
I have subclassed QAbstractListModel and viewing my model using QListView. My only problem is I can edit all my rows in the model. Some of them are editable but some are not and some times the scrollbar is also not working. Any help will be appreciated. Thanks in advance.
Regards,
Chandan





#ifndef PATIENT_MODEL_H
#define PATIENT_MODEL_H



#include <QAbstractListModel>
#include <QList>

class CCameraDisplayManager;
class CPatientListModel : public QAbstractListModel{
//, parent(), rowCount(), columnCount(), and data()
Q_OBJECT

private:
// private statics

// private members
CCameraDisplayManager *m_pCameraDiplayManager;
QStringList m_StringList;

protected:
// protected members

private:
// private methods - function declarations

protected:
// protected methods - function declarations


///////////////////////////////////////////////////
// public interface
///////////////////////////////////////////////////

public:
// public members

public:
// public methods - include class constructors and destructor function declarations here

public:
/* StringListModel(const QStringList &strings, QObject *parent = 0)
: QAbstractListModel(parent), stringList(strings) {}

int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;*/
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
bool insertRows(int position, int rows, const QModelIndex &parent);
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole);

CPatientListModel();
~CPatientListModel();
};

#endif






cpp file



#include "PatientModel.h"

/////////////////////////////////////////////////////////////////////////////////
// static member defintions
/////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////
// private methods - function definitions & private constructor / destructor
/////////////////////////////////////////////////////////////////////////////////
// private methods - function declarations
/////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////
// protected methods - function definitions
/////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////
// public methods - function definitions
/////////////////////////////////////////////////////////////////////////////////

int CPatientListModel::rowCount(const QModelIndex &parent) const
{
// returns the number of row in the list

return m_StringList.count();
}

QVariant CPatientListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();

if (index.row() >= m_StringList.size())
return QVariant();

if (role == Qt::DisplayRole)
return m_StringList.at(index.row());
else
return QVariant();
}

Qt::ItemFlags CPatientListModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;

return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}

bool CPatientListModel::insertRows(int iPosition, int iRows, const QModelIndex &parent)
{
beginInsertRows(QModelIndex(), iPosition, iPosition+iRows-1);

for (int iRow = 0; iRow < iRows; ++iRow) {
m_StringList.insert(iPosition, "");
}

endInsertRows();
return true;
}

bool CPatientListModel::setData(const QModelIndex &index,
const QVariant &value, int iRole)
{
if (index.isValid() && iRole == Qt::EditRole) {

m_StringList.replace(index.row(), value.toString());
emit dataChanged(index, index);
return true;
}
return false;
}

/////////////////////////////////////////////////////////////////////////////////
// public class constructors and destructor
/////////////////////////////////////////////////////////////////////////////////

CPatientListModel::CPatientListModel(){

}

CPatientListModel::~CPatientListModel(){

}


callser function