I have a QAbstractItemModel-based class that wraps an external tree-shaped data structure. The nodes in the tree use boost::shared_ptr<> because multiple nodes may point to the same instance. (Think a property-value structure, where multiple nodes might have the property "units" with value "cm". The instance containing the "units"-"cm" pair is stored in a shared pointer).

I want to store these shared pointers in the QModelIndex instances I create using createIndex(). I am getting myself completely confused about how to do that.

I tried to use QModelIndex::internalPointer(), but C++ doesn't allow the shared_ptr to be cast to a void *. I also read another post here advising against using internalPointer().

I am guessing that the correct way is to use the model's data()/setData() or itemData()/setItemData() methods with a Qt::UserRole. I had already implemented the data() method on my model to return strings for the Qt::DisplayRole, but this was when I was using QModelIndex::internalPointer() and bare C++ pointers (before I changed the external model to use shared pointers).

The documentation for QAbstractItemModel::itemData() says
Reimplement this function if you want to extend the default behavior of this function to include custom roles in the map.
Fine, but what does that mean exactly? Do I need to implement my own data structure for storing the shared pointer in association with the QModelIndex instance?

I have been unsuccessful in my searching to find an example that does not use QStandardItem / QStandardItemModel as a basis.

Can someone please explain how this works (or point me to a link that does)?