Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAbstractItemModel & shared_ptr<> issue... itemFromIndex()

QAbstractItemModel & shared_ptr<> issue... itemFromIndex()

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    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

    kshegunovK 1 Reply Last reply
    0
    • D Dariusz

      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

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @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?

      Read and abide by the Qt Code of Conduct

      D 1 Reply Last reply
      0
      • kshegunovK kshegunov

        @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?

        D Offline
        D Offline
        Dariusz
        wrote on last edited by
        #3

        @kshegunov Its pretty much my implementation of https://code.woboq.org/qt5/qtbase/src/gui/itemmodels/qstandarditemmodel.cpp.html#_ZNK18QStandardItemModel13itemFromIndexERK11QModelIndex

        kshegunovK 1 Reply Last reply
        0
        • D Dariusz

          @kshegunov Its pretty much my implementation of https://code.woboq.org/qt5/qtbase/src/gui/itemmodels/qstandarditemmodel.cpp.html#_ZNK18QStandardItemModel13itemFromIndexERK11QModelIndex

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          The problem is, your implementation is different from what you sourced (and most probably wrong, unfortunately). My question stands, though, what do you keep in the internal pointer of the model index?

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dariusz
            wrote on last edited by
            #5

            @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?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dariusz
              wrote on last edited by Dariusz
              #6

              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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved