如何在QAbstractItemView中获取可见的QModelIndex列表 [英] How to get list of visible QModelIndex in QAbstractItemView

查看:515
本文介绍了如何在QAbstractItemView中获取可见的QModelIndex列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以获取QAbstractItemView中当前可见项目的列表?并且,如果可能的话,会收到有关更改此列表的任何通知.

Is there any way to get a list of currently visible items in QAbstractItemView? And, if it possible, to receive any notifications about changing of this list.

已更新: 我要问的是关于非普通结构而不是QTableViewQAbstractItemViewQTreeView.

Upd: I'm asking exactly about QAbstractItemView or QTreeView with non-plain structure, not QTableView.

Upd2: 我正在实现带有复选框的树视图模型.我想要下一个行为(与检查/取消锁定相同):

Upd2: I'm implementing tree view model with checkboxes. I want next behavior (same for checking/uncheking):

  • 如果选中了其中一​​个复选框,则必须选中所有孩子
  • 如果所有子复选框均已选中-则父复选框也应选中.父母的父母也一样,依此类推...

检查状态由外部数据源监视/修改,因此我需要一种机制来更新所有更改的子代/父代. dataChanged信号对我来说还不够,因为构建所有已更改的QModelIndex列表以进行更新非常繁琐.而且根本没有必要,因为所有新数据都将从QAbstractItemModel::data中提取.

Check state are monitored/modified by external data source, so I need a mechanism to update all changed children/parents. dataChanged signal is not enough for me because it is very expansive to build a list of all changed QModelIndex for updating. And it is not necessary at all, because all fresh data will be picked from QAbstractItemModel::data.

我发现下一个肮脏的骇客会更新所有项目:emit dataChanged( QModelIndex(), QModelIndex() );,但没有针对无效索引的记录.

I found next dirty hack to update all items: emit dataChanged( QModelIndex(), QModelIndex() ); but it's undocumented for invalid indexes.

因此,我需要一种方法来强制所有可见项目重新绘制它们并包含新数据.

So, I need a way to force all visible items redraw they content with fresh data.

推荐答案

我认为在任何情况下都不需要可见项列表.在正确实施模型的情况下,所有项目都会自动更新. 实施的难点-迫使孩子和父母更新.我写了以下代码:

I think that there are no cases where list of visible items is requred. In case of correct model implementation all items are updated automatically. Hard part of implementation - force children and parents to update. I wrote following code:

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 );
            }
        }

这篇关于如何在QAbstractItemView中获取可见的QModelIndex列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆