• 3213阅读
  • 2回复

[提问]QAbstractTableModel 子类的项的添加和删除信号? [复制链接]

上一主题 下一主题
离线xins0123
 

只看楼主 倒序阅读 楼主  发表于: 2015-03-17
一个继承于QAbstractTableModel 的子类,子类中用户QList保存数据,实现了data()函数,我想接收这个模型添加一项或者删除一项的信息,帮助中有rowsInserted() 和 rowsRemoved() ,但连接这两个信号的槽却没有调用,帮助中好像说这两个信号是QAbstractItemModel emit 的,子类不能 emit
“It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.”

请问怎么处理这两个信号?或者有其他的信号可连接?
离线dbzhang800

只看该作者 1楼 发表于: 2015-03-18
信号没被 emit ,说明你的子类实现有问题。

An insertRows() implementation must call beginInsertRows() before inserting new rows into the data structure, and endInsertRows() immediately afterwards.
An insertColumns() implementation must call beginInsertColumns() before inserting new columns into the data structure, and endInsertColumns() immediately afterwards.
A removeRows() implementation must call beginRemoveRows() before the rows are removed from the data structure, and endRemoveRows() immediately afterwards.
A removeColumns() implementation must call beginRemoveColumns() before the columns are removed from the data structure, and endRemoveColumns() immediately afterwards.
离线xins0123

只看该作者 2楼 发表于: 2015-03-18
virtual int rowCount(const QModelIndex& parent) const;
virtual int columnCount(const QModelIndex& parent) const;
virtual QVariant data(const QModelIndex& index, int role) const;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;

我实现这几个接口,能正常现在显示。还需要实现什么接口呢?
昨晚看了另一篇文章,按文章说的实现了另外两个接口
virtual bool insertRows(int row, int count, const QModelIndex& parent);
virtual bool removeRows(int row, int count, const QModelIndex& parent);

实现直接调用
beginInsertRows(parent, row + 1, row + count);
endInsertRows();
return true;

beginRemoveRows(parent, row , row + count - 1);
endRemoveRows();
return true;
但也是不行,因为我已经实现了data() 和 rowCount(),根据不会调用到insertRows() 这接口
快速回复
限100 字节
 
上一个 下一个