简体   繁体   中英

Qt - Getting a QModelIndex without a View

I have a QAbstractModel which is basically a tree structure derived from the Qt EditableTreeModel example. 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.

The problem is ascertaining the QModelIndex when you need to refresh a section that is not at the root. Qt seems to only provide QModelIndexes in relation to view events like clicking. 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.

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).

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.

Alternatively, are there options to have a model and a non-visual View to enable the getting of 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. Use the parent parameter if you want to find an index which is not at the root.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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