有没有办法在QAbstractItemView
中获得当前可见项目的列表?并且,如果可能的话,接收关于该列表的改变的任何通知。
更新:我问的是具有非纯结构的QAbstractItemView
或QTreeView
,而不是QTableView
。
Upd2:我正在实现带有复选框的树视图模型。我想要下一个行为(与选中/取消选中相同):
检查状态由外部数据源监视/修改,因此我需要一种机制来更新所有更改的子项/父项。dataChanged
信号对我来说是不够的,因为构建所有更改的QModelIndex
的列表以进行更新是非常广泛的。而且根本没有必要,因为所有的新数据都将从QAbstractItemModel::data
中挑选出来。
我发现了下一个更新所有项的肮脏黑客:emit dataChanged( QModelIndex(), QModelIndex() );
,但它没有记录无效索引。
因此,我需要一种方法来强制所有可见的项目重新绘制他们的内容与新的数据。
发布于 2013-04-08 17:01:44
我认为不存在重新请求可见项列表的情况。在正确实现模型的情况下,所有项目都会自动更新。实现的难点--强制孩子和家长更新。我写了以下代码:
bool TreeModel::setData( const QModelIndex &index, const QVariant &value, int role )
case Qt::CheckStateRole:
{
TreeItemList updateRangeList; // Filled with items, in which all childred must be updated
TreeItemList updateSingleList; // Filled with items, which must be updated
item->setCheckState( value.toBool(), updateRangeList, updateSingleList ); // All magic there
foreach ( TreeAbstractItem *i, updateRangeList )
{
const int nRows = i->rowCount();
QModelIndex topLeft = indexForItem( i->m_childs[0] );
QModelIndex bottomRight = indexForItem( i->m_childs[nRows - 1] );
emit dataChanged( topLeft, bottomRight );
}
foreach ( TreeAbstractItem *i, updateSingleList )
{
QModelIndex updateIndex = indexForItem( i );
emit dataChanged( updateIndex, updateIndex );
}
}
发布于 2013-04-05 01:44:20
您可以通过调用以下命令获得右上角和右下角的单元格:
tableview->indexAt(tableview->rect().topLeft())
tableview->indexAt(tableview->rect().bottomRight())
要获得更改通知,请重新实现qabstractscrollarea的虚拟函数
scrollContentsBy
此函数在查看端口滚动时调用。调用QTableView::scrollContentsBy,然后执行所需的任何操作。
发布于 2014-03-14 03:58:55
对于QTreeView
,可见项列表可以像这样遍历:
QTreeView& tv (yourTreeView);
// Get model index for first visible item
QModelIndex modelIndex = tv.indexAt(tv.rect().topLeft());
while (modelIndex.isValid())
{
// do something with the item indexed by modelIndex
...
// This navigates to the next visible item
modelIndex = tv.indexBelow(modelIndex);
}
https://stackoverflow.com/questions/15817429
复制相似问题