关于QAbstractItemModel::data () 函数的问题,请老鸟们帮忙看一下

weixin_38091030 2019-09-19 05:37:29
模型视图编程,视图是一个QTableVew实例,模型是自定义的继承自QAbstractTableModel类,但是编译时会出现错误—— 错误:passing 'const ObservedLine' as 'this' argument of 'QString ObservedLine::name()' discards qualifiers 代码如下: //mymodel.hclass ObLineListModle : public QAbstractTableModel{   Q_OBJECTpublic:    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.cppObLineListModle::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;       //观测线名称};
...全文
117 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复

435

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧