At the moment I am working on a Remote Registry Editor for Windows Mobile devices under Linux. I am writing this application in PyQt4, though my question is not related to the Python part, but Qt in general, hence I am asking it here.

I would like to show the registry key structure in a QTreeView. In order to do this, I created a RegistryModel, which is subclassed from the QAbstractItemModel. Other classes I created are:
* RegistryKey. This class contains all sub keys and all values for a particular key in the registry. The RegistryModel gets the actual data from the Registrykey object that corresponds to the item in the model.
* Registry. This class takes care of all the communication with other layers of my program (i.e. communication required for fetching all keys / values of a particular key). Each of the RegistryKey objects has a reference to this object.

Because fetching the complete tree structure of the keys already takes more than 5 minutes, I would like to do this in a more dynamic way.

What I would like to do is the following:
1. User expands a registry key in the QTreeView for which the underlying model has no data yet. Whenever no data is known, the underlying model will have one subkey, namely one with the name "Fetching...".
2. The user sees one subkey under the just expanded subkey: "Fetching..."
3. The underlying RegistryKey object will fetch the data from the device, via the Registry object
4. After all data has been fetched the Registry object provides the RegistryKey object with the new data
5. The RegistryKey object first removes its only sub key (i.e. the "Fetching..." sub-key)
6. The RegistryKey object creates new RegistryKey objects for each of its sub keys and add these as children
7. The enduser must see that the "Fetching..." subkey vanishes, while the subkeys under the key he expanded should appear instead.


Everything upto and including step 4 I understand. From the signal emitted I obtain a QModelIndex, from which I can determine which RegistryKey object corresponds to the item the user selected. I can then do all the stuff I need to do to get the actual contents from the device.

After this, with step 5, I am running into problems with not knowing how to model this correctly. If I understood correctly, whenever you change the underlying model you would have to call beginInsertRows() on the RegistryModel with the corresponding QModelIndex you are about to add child-keys to, followed by the actual adding of the keys and finally a endInsertRows(). The same holds for deleting the "Fetching..." sub-key, but then with the beginRemoveRows and endRemoveRows.

My main question is now, what is the best/nicest way of determining which QModelIndex corresponds to a given RegistryKey object? My first idea was to make create a dictionary of RegistryKey -> QModelIndex, but somewhere I read that you should not keep refrences to a QModelIndex, but discard it as soon as possible after use again.

I have looked at all kinds of examples, but still have not found any that show me how to get from an object in the underlying data model back to a QModelIndex that I can use. Is there actually a way to do this correctly, or is my original setup of classes not ok for this? Is it actually OK to have an underlying data model under the QAbstractItemModel that can be changed via some other way besides the QTreeView?

If more information is needed, please let me know and I will try to provide it.

Kind regards,

Guido Diepen