QModelIndex::constInternalPointer returns nullptr unexpectedly
-
-
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::constInternalPointerin return value ofMyModel::indexis always non-null, how couldassert(p_parent_data != nullptr)in the above codes fail?
-
-
-
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::constInternalPointerin return value ofMyModel::indexis always non-null, how couldassert(p_parent_data != nullptr)in the above codes fail?
@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 * parentis 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 ofparentbe unreliable. Print out the value ofparenton the line before theassert().Try making your
void * parentbeconst 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 & parentwhile the left-hand side uses the newvoid * parentdeclaration. 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.... -
-
@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 * parentis 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 ofparentbe unreliable. Print out the value ofparenton the line before theassert().Try making your
void * parentbeconst 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 & parentwhile the left-hand side uses the newvoid * parentdeclaration. 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....@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 columnsMyModel::parentalways returnsQModelIndex() -
@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 * parentis 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 ofparentbe unreliable. Print out the value ofparenton the line before theassert().Try making your
void * parentbeconst 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 & parentwhile the left-hand side uses the newvoid * parentdeclaration. 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....@JonB said in QModelIndex::constInternalPointer returns nullptr unexpectedly:
"It can't!"
Alright
The problem for me is that not only each
QModelIndexin the first column may have parent
but eachQModelIndexin the remaining columns should also have the same parent as theQModelIndexin the first column of the same row.Thanks