简体   繁体   English

Qt-获取没有视图的QModelIndex

[英]Qt - Getting a QModelIndex without a View

I have a QAbstractModel which is basically a tree structure derived from the Qt EditableTreeModel example. 我有一个QAbstractModel,它基本上是从Qt EditableTreeModel示例派生的树结构。 I've adapted it to show a tree structure which contains abstract data objects and is to be only updated programmatically, never by the user via a View. 我已经对其进行了修改,以显示一个包含抽象数据对象的树结构,并且只能通过编程方式对其进行更新,而用户永远不会通过View对其进行更新。

The problem is ascertaining the QModelIndex when you need to refresh a section that is not at the root. 问题是在需要刷新不在根目录中的节时确定QModelIndex。 Qt seems to only provide QModelIndexes in relation to view events like clicking. Qt似乎只提供与单击事件等视图事件有关的QModelIndexes。 My model can very easily find the subsection it needs to refresh, but seems to have no view-independent methods of getting the QModelIndex needed for insertRows, removeRows, etc. 我的模型可以很容易地找到需要刷新的子节,但是似乎没有与视图无关的方法来获取insertRows,removeRows等所需的QModelIndex。

This question had one response suggesting iteration through data items to find a QModelIndex, but I can't see a way to View-independently define the root to a QPersistentModelIndex (as this says you can't just use the default QModelIndex constructor for root children). 这个问题有一个建议,建议通过数据项进行迭代以找到QModelIndex,但是我看不到一种视图独立地将根定义为QPersistentModelIndex的方法(因为说明您不能仅对根子级使用默认的QModelIndex构造函数)。

So, in a nutshell, how do I either find a QModelIndex without needing View related events, or define the root as a QPersistentModelIndex programatically, again without a View or user input. 因此,简而言之,如何在不使用View相关事件的情况下找到QModelIndex,或者以编程方式将根定义为QPersistentModelIndex,又又没有View或用户输入。

Alternatively, are there options to have a model and a non-visual View to enable the getting of QModelIndexes? 或者,是否可以选择具有模型和非可视视图以启用QModelIndexes? I need the model to be up to date whether a view is shown or not. 无论是否显示视图,我都需要模型是最新的。

Cheers all 欢呼全部

You can use QAbstractItemModel::index(int row, int column, const QModelIndex & parent) to get a QModelIndex directly from your model. 您可以使用QAbstractItemModel::index(int row, int column, const QModelIndex & parent)从模型中直接获取QModelIndex Use the parent parameter if you want to find an index which is not at the root. 如果要查找不在根目录下的索引,请使用parent参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM