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)
DMapLayerModel model(obj)
To copy to clipboard, switch view to plain text mode
The file DMapLayerModel.h is:
#ifndef DMAPLAYERMODEL_H_
#define DMAPLAYERMODEL_H_
#include <QtCore>
#include <QtGui>
#include "DMapLayerObject.h"
Q_OBJECT
public:
DMapLayerModel
(DMapLayerObject
*layers,
QObject *parent
= 0);
virtual ~DMapLayerModel();
QVariant headerData
(int section, Qt
::Orientation orientation,
int role = Qt::DisplayRole) const;
int role = Qt::EditRole);
private:
DMapLayerObject *layers;
};
#endif /* DMAPLAYERMODEL_H_ */
#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_ */
To copy to clipboard, switch view to plain text mode
and the DMapLayerModel.cpp is:
#include "DMapLayerModel.h"
DMapLayerModel
::DMapLayerModel(DMapLayerObject
*layers,
QObject *parent
)}
DMapLayerModel::~DMapLayerModel(){
}
int DMapLayerModel
::rowCount(const QModelIndex &parent
) const{ int count=layers->getLayersNameList()->count();
return count;
}
if (!index.isValid())
if (index.row() >=layers->getLayersNameList()->size())
if (role == Qt::DisplayRole)
return QVariant::fromValue(layers
->getLayersNameList
()->at
(index.
row()));
else
}
QVariant DMapLayerModel
::headerData(int section, Qt
::Orientation orientation,
int role) const{
if (role != Qt::DisplayRole)
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;
}
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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
I'm a bit confused.
Thanks in advance.
M.
Bookmarks