QAbstractItemModel & shared_ptr<> issue... itemFromIndex()
-
Hey
I'm trying to update my treeView to be based on shared_ptrs<> but I hit quite a few walls with it. For some reason after working for a while now its broken and I'm just at loss...
I made my own QTreeItem - not inheriting any Qt object and then went ahead and gave it to the QAbstractItemModel.
Further down the line I'm trying to cast the index.internalPointer back to my item, and it somehow becomes null or something. I have no idea, its all black magic at this point!
std::shared_ptr<myTestNode> node = *static_cast< std::shared_ptr<myTestNode> * >(index.internalPointer());
I though that the code above should always be valid, but when I then try to print it or something, it appears to be a "bad item" or something. When I debug it, most of its members are <Unable to read memory>
The crash happens here>
std::shared_ptr<myTestNode> node = itemFromIndex(index) node->getParentNode(); ... std::shared_ptr<myTestNode> getParentNode(){ return mParentNode; } //// Error reads : memory.h void _Incref() { // increment use count _MT_INCR(_Uses); } //// Fairly sure this happens because the passed parentPtr is nullptr... because node in 1st place was a nullptr/bad ptr returned from itemFromIndex();
Can any1 help out with shared_ptr implementation on this system?
TIA
-
@Dariusz said in QAbstractItemModel & shared_ptr<> issue... itemFromIndex():
std::shared_ptr<myTestNode> node = *static_cast< std::shared_ptr<myTestNode> * >(index.internalPointer());
This line really makes no sense. As a spinoff, what are you keeping in the model index's internal pointer?
-
@kshegunov Its pretty much my implementation of https://code.woboq.org/qt5/qtbase/src/gui/itemmodels/qstandarditemmodel.cpp.html#_ZNK18QStandardItemModel13itemFromIndexERK11QModelIndex
-
@Dariusz said in QAbstractItemModel & shared_ptr<> issue... itemFromIndex():
std::shared_ptr<myTestNode> node = itemFromIndex(index)
hey
I'm a tad lost. Here is my itemFromIndex
std::shared_ptr<myTestNode> itemFromIndex(QModelIndex index){ if(!index.isValid()) { return mRootItem; } std::shared_ptr<myTestNode> node = *static_cast< std::shared_ptr<myTestNode> * >(index.internalPointer()); return node; }
and getParentNode();
std::shared_ptr<myTestNode> getParentNode(){ return mParentNode; }
From what I can tell, for some reason the static_cast is returning wrong item or something like that.
I did not touch QModelIndex system,
As to
"internal pointer of the model index?"
I have no idea, o.O How do I deal with that? Can you ping me to docs/explain maybe? -
Ok I might have... found an issue with my implementation, turns out the issue might have been in createIndex() function when making indexes.
Esentially at the moment I did this createIndex(row, 0, node.get()); // To get the raw pointer from shared_ptr, and I'm fairly sure this is the hikki. Because later when I get itemFromIndex, I get rawPointer not a shared_ptr<>... hmm.
But if I just do createIndex(row, 0, node) then I get error C2664: 'QModelIndex QAbstractItemModel::createIndex(int,int,quintptr) const': cannot convert argument 3 from 'std::shared_ptr<myTestNod>' to 'void *'
So this is now a bit of a hmmm
Feels like I need this instead:
createIndex(row, 0, &node)
Pass a pointer to a shared_ptr<> to object.I'm still having problems tho. Hmmm