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. QModelIndex::constInternalPointer returns nullptr unexpectedly

QModelIndex::constInternalPointer returns nullptr unexpectedly

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 437 Views
  • 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.
  • jronaldJ Offline
    jronaldJ Offline
    jronald
    wrote on last edited by jronald
    #1
    • code

      QModelIndex MyModel::index(int row, int column, const QModelIndex & parent) const
      {
          if (parent.isValid())
          {
              const void * p_parent_data = parent.constInternalPointer();
              assert(p_parent_data != nullptr);
              ...
              assert(p != nullptr);
              return createIndex(row, column, p);
          }
          else
          {
              ...
              assert(p != nullptr);
              return createIndex(row, column, p);
          }
      }
      
    • Question
      QModelIndex::constInternalPointer in return value of MyModel::index is always non-null, how could assert(p_parent_data != nullptr) in the above codes fail?

    JonBJ 1 Reply Last reply
    0
    • jronaldJ jronald
      • code

        QModelIndex MyModel::index(int row, int column, const QModelIndex & parent) const
        {
            if (parent.isValid())
            {
                const void * p_parent_data = parent.constInternalPointer();
                assert(p_parent_data != nullptr);
                ...
                assert(p != nullptr);
                return createIndex(row, column, p);
            }
            else
            {
                ...
                assert(p != nullptr);
                return createIndex(row, column, p);
            }
        }
        
      • Question
        QModelIndex::constInternalPointer in return value of MyModel::index is always non-null, how could assert(p_parent_data != nullptr) in the above codes fail?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @jronald said in QModelIndex::constInternalPointer returns nullptr unexpectedly:

      how could assert(parent) in the above codes fail?

      "It can't!"

      How do you know void * parent is non-null? If you are looking at it in debugger watch window, don't rely on that, it could be being held in a register and the value of parent be unreliable. Print out the value of parent on the line before the assert().

      Try making your void * parent be const void * parent (I'm surprised you don't get a compiler warning)?

      Meanwhile I find your

      void * parent = parent.constInternalPointer();
      

      "strange". The right-hand side uses the const QModelIndex & parent while the left-hand side uses the new void * parent declaration. This is at best "confusing". Suggest rename the local pointer variable to something else.

      Finally, are you sure you are compiling for debug? Else assert() is a no-op....

      jronaldJ 2 Replies Last reply
      2
      • JonBJ JonB

        @jronald said in QModelIndex::constInternalPointer returns nullptr unexpectedly:

        how could assert(parent) in the above codes fail?

        "It can't!"

        How do you know void * parent is non-null? If you are looking at it in debugger watch window, don't rely on that, it could be being held in a register and the value of parent be unreliable. Print out the value of parent on the line before the assert().

        Try making your void * parent be const void * parent (I'm surprised you don't get a compiler warning)?

        Meanwhile I find your

        void * parent = parent.constInternalPointer();
        

        "strange". The right-hand side uses the const QModelIndex & parent while the left-hand side uses the new void * parent declaration. This is at best "confusing". Suggest rename the local pointer variable to something else.

        Finally, are you sure you are compiling for debug? Else assert() is a no-op....

        jronaldJ Offline
        jronaldJ Offline
        jronald
        wrote on last edited by jronald
        #3

        @JonB
        I've refined the code.

        Run in debug mode, and assert(p_parent_data != nullptr) fails.

        The problem might be in QModelIndex MyModel::parent(const QModelIndex & child) const
        because there are 3 columns and only the first column might have children, for other columns MyModel::parent always returns QModelIndex()

        1 Reply Last reply
        0
        • JonBJ JonB

          @jronald said in QModelIndex::constInternalPointer returns nullptr unexpectedly:

          how could assert(parent) in the above codes fail?

          "It can't!"

          How do you know void * parent is non-null? If you are looking at it in debugger watch window, don't rely on that, it could be being held in a register and the value of parent be unreliable. Print out the value of parent on the line before the assert().

          Try making your void * parent be const void * parent (I'm surprised you don't get a compiler warning)?

          Meanwhile I find your

          void * parent = parent.constInternalPointer();
          

          "strange". The right-hand side uses the const QModelIndex & parent while the left-hand side uses the new void * parent declaration. This is at best "confusing". Suggest rename the local pointer variable to something else.

          Finally, are you sure you are compiling for debug? Else assert() is a no-op....

          jronaldJ Offline
          jronaldJ Offline
          jronald
          wrote on last edited by jronald
          #4

          @JonB said in QModelIndex::constInternalPointer returns nullptr unexpectedly:

          "It can't!"

          Alright

          The problem for me is that not only each QModelIndex in the first column may have parent
          but each QModelIndex in the remaining columns should also have the same parent as the QModelIndex in the first column of the same row.

          Thanks

          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