模型视图编程,视图是一个QTableVew实例,模型是自定义的继承自QAbstractTableModel类,但是编译时会出现错误——
错误:passing 'const ObservedLine' as 'this' argument of 'QString ObservedLine::name()' discards qualifiers
代码如下:
//mymodel.h
class ObLineListModle : public QAbstractTableModel
{
Q_OBJECT
public:
explicit ObLineListModle(QObject *parent = 0);
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
private:
QList<ObservedLine> obLineList; //自定义数据类
};
//myModel.cpp
ObLineListModle::ObLineListModle(QObject *parent) : QAbstractTableModel(parent)
{
}
int ObLineListModle::rowCount(const QModelIndex &parent) const
{
return obLineList.count();
}
void ObLineListModle::modelInit(QList<ObservedLine> list)
{
this->obLineList = list;
}
int ObLineListModle::columnCount(const QModelIndex &parent) const
{
return 1;
}
QVariant ObLineListModle::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
int row=index.row();
int column=index.column();
if(row>=obLineList.count())
return QVariant();
switch(column)
{
case 0:
return this->obLineList.at(row).name(); //错误就在这一行
default:
return QVariant();
}
}
//自定义数据类
class ObservedLine
{
public:
ObservedLine();
QString name();
private:
QString _name; //观测线名称
};