PDA

View Full Version : implementation of QabstractItemModel problem



mikelantonio
7th August 2009, 09:32
Hi,
I'm using qt4 from few weeks.
I've read the model subclassing reference page, and I've wrote my model.
The problem is that the compiler says: "cannot declare variable ‘model’ to be of abstract type ‘DMapLayerModel" on this instruction

DMapLayerModel model(obj)

The file DMapLayerModel.h is:



#ifndef DMAPLAYERMODEL_H_
#define DMAPLAYERMODEL_H_

#include <QtCore>
#include <QtGui>
#include "DMapLayerObject.h"

class DMapLayerModel : public QAbstractItemModel{
Q_OBJECT
public:
DMapLayerModel(DMapLayerObject *layers, QObject *parent = 0);
virtual ~DMapLayerModel();
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;
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole);
bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());

private:
DMapLayerObject *layers;

};

#endif /* DMAPLAYERMODEL_H_ */


and the DMapLayerModel.cpp is:



#include "DMapLayerModel.h"

DMapLayerModel::DMapLayerModel(DMapLayerObject *layers, QObject *parent)
: QAbstractItemModel(parent), layers(layers){
}


DMapLayerModel::~DMapLayerModel(){

}

int DMapLayerModel::rowCount(const QModelIndex &parent) const{
int count=layers->getLayersNameList()->count();
return count;
}

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

if (index.row() >=layers->getLayersNameList()->size())
return QVariant();

if (role == Qt::DisplayRole)
return QVariant::fromValue(layers->getLayersNameList()->at(index.row()));
else
return QVariant();

}

QVariant DMapLayerModel::headerData(int section, Qt::Orientation orientation,
int role) const{

if (role != Qt::DisplayRole)
return QVariant();

if (orientation == Qt::Horizontal)
return layers->getName();
else
return layers->getName();
}

Qt::ItemFlags DMapLayerModel::flags(const QModelIndex &index) const{

if (!index.isValid())
return Qt::ItemIsEnabled;

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

}

bool DMapLayerModel::setData(const QModelIndex &index, const QVariant &value,
int role){

if (index.isValid() && role == Qt::EditRole) {
layers->getLayersNameList()->replace(index.row(), value.toString());
emit dataChanged(index, index);
return true;
}
return false;
}

bool DMapLayerModel::insertRows(int position, int rows, const QModelIndex &index){
return true;
}

bool DMapLayerModel::removeRows(int position, int rows, const QModelIndex &index){
return true;
}


I'm a bit confused.
Thanks in advance.
M.

numbat
7th August 2009, 10:14
That error means that you've haven't implemented a pure virtual function. Pure virtual functions are identified by having "= 0" in the prototype (as seen in the documentation). In this case the missing function is columnCount. If your model is a simple string list you may be interested in QStringListModel. You may also want to use Model Test (http://labs.trolltech.com/page/Projects/Itemview/Modeltest) with your custom model.

mikelantonio
7th August 2009, 16:31
thanks, it works. :)
I need to implement, other than columnCount, either parent and index methods.