Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

[SOLVED] How to use QModelIndex internal pointer in specific case



  • To keep track of objects in my game, I use a global array of pointers, and then a class ObjectHandle to locate an object. An ObjectHandle is simply a struct with two integers: index into the global array, and an object ID. In this way, ObjectHandles can refer to an object in the global array, and if the object is deleted, ObjectHandle::toObject simply returns nullptr. ObjectHandles are small and are passed around by value or const ref.

    Now, I want to create a tree model of my objects. How can I use the internal pointer of QModelIndex with my handles? I don't want to feed it Object pointers directly, since the whole point of my index-in-global-pointer-array is that the handles should always be used to refer to them.

    I could pass a pointer to a heap created ObjectHandle to QAbstractItemModel::createIndex, but then I wouldn't know when to delete it.

    All comments welcome!



  • Comments still welcome, but solved it by letting the model own a cache of ObjectHandles, feeding their addresses to createIndex as needed. After a cup of coffee the solution was pretty obvious. :)


Log in to reply