如何将 QModelIndex 与新行关联?
我已经编写了一个 QAbstractListModel ,其模型索引包含一个我处理数据绝对需要的指针。我像这样添加数据:
void PointListModel::addPoint(int frameNumber, QPoint const& pos)
{
PointItem *pointItem = new PointItem( frameNumber, pos );
QModelIndex newRow = this->createIndex( m_points.count(), 0, pointItem );
qDebug() << newRow.internalPointer();
beginInsertRows( newRow, m_points.count(), m_points.count() );
m_points.insert( m_points.count( ), pointItem );
endInsertRows();
emit pointAdded( pointItem, pos );
}
直到后来我才意识到 beginInsertRows
的参数要求的是新行的 parent 模型索引,而不是新行的实际索引型号索引。
因此,此时,Qt 无法提供与此特定行关联的 QModelIndex
。如何为这个新行创建自己的模型索引?
I've cooked up a QAbstractListModel
whose model indexes contain a pointer I absolutely needed in order to process data. I add the data like so:
void PointListModel::addPoint(int frameNumber, QPoint const& pos)
{
PointItem *pointItem = new PointItem( frameNumber, pos );
QModelIndex newRow = this->createIndex( m_points.count(), 0, pointItem );
qDebug() << newRow.internalPointer();
beginInsertRows( newRow, m_points.count(), m_points.count() );
m_points.insert( m_points.count( ), pointItem );
endInsertRows();
emit pointAdded( pointItem, pos );
}
It was only later that I realized that the argument to beginInsertRows
is asking for the parent model index of the new row, not the new row's actual model index.
So, at this point in time, Qt has given me no way of supplying a QModelIndex
to associate with this particular row. How do I create my own model index for this new row?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我正在重写我的答案,因为经过一番研究后我发现我错了。
添加新数据时,您不应该执行任何特殊操作来创建新索引。您的代码应如下所示:
然后您应该实现
index()
方法(该方法将根据需要创建索引)和parent()
方法(该方法将确定某些索引的父级) ,但由于您有一个列表模型,它可能应该总是返回QModelIndex()
。这是一篇关于创建自定义模型的好文章。下面是一个工作
QAbstractListModel
的完整示例:Okay, I'm rewriting my answer as after some research I've found out that I got it wrong.
You shouldn't do anything special to create a new index when you add new data. You code should look like this:
Then you should implement the
index()
method which will create indexes on demand and theparent()
method which will determine the parent of some index, but since you have a list model, it should probably always just returnQModelIndex()
. Here is a good article about creating custom models.Here is a complete example of a working
QAbstractListModel
:如果您从错误的要求开始,最终会得到错误的解决方案:)
list 模型足够简单,因此您不需要除
QModelIndex
的row()
唯一定义索引地址的数据。因此,给定一个
QModelIndex
mi
,当您之前这样做时,您可以
在
plm
是您的PointListModel
的地方执行操作。如果您需要访问 PointItem 时没有指向它的指针,则可以像这样重建它:反过来,PointListMode::pointItemFromIndex()会做实际的工作:
这是在 Qt 中使用
QAbstractListModel
时要意识到的最重要的事情:在心里用int row
替换QModelIndex
,忽略它拥有的其他所有内容(无效的QModelIndex
具有row() == -1
)。对于
QAbstractTableModel
也是如此:在心里将QModelIndex
减少到int row, int column
。忘记其他一切。唯一需要完整的
QModelIndex
(包括其internalPointer()
或internalId()
)是当您实现树模型时(QAbstractItemModel
)。If you start with wrong requirements, you end up with wrong solutions :)
A list model is simple enough so that you don't need more than the
QModelIndex
'srow()
to uniquely define the data the index addresses.So, given a
QModelIndex
mi
, when you before didyou can instead do
where
plm
is yourPointListModel
. If you don't have a pointer to it lying around when you need to access thePointItem
, you can reconstruct it like this:In turn,
PointListMode::pointItemFromIndex()
would do the actual work:This is the most important thing to realize when working with
QAbstractListModel
in Qt: Mentally replaceQModelIndex
withint row
, ignore everything else it has (an invalidQModelIndex
hasrow() == -1
).Same for
QAbstractTableModel
: mentally reduce theQModelIndex
toint row, int column
. Forget everything else.The only time you need the full
QModelIndex
(including itsinternalPointer()
orinternalId()
is when you implement a tree model (QAbstractItemModel
).