Solved Why is my table model (based on QAbstractTableModel) asked for header section (index) that is out of range?
-
Hello awesome Qt-community,
I'm using Qt 6.0.1 for Desktop development
I have a table model based on QAbstractTableModel that holds its header information as a member QList<QString> m_Header.
I implemented columnCount like this:
int TableModel::columnCount(const QModelIndex &parent) const { if (parent.isValid()) { qWarning() << "Invalid columnCount parent"; return 0; } return m_Header.size(); }
And I implemented headerData like this:
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole) { return QVariant(); } if (section < 0 || section >= m_Header.size()) { qWarning() << "Invalid headerData section" << section; return QVariant(); } if (orientation == Qt::Orientation::Horizontal) { return QVariant(m_Header[section]); } else if (orientation == Qt::Orientation::Vertical) { return QVariant("empty"); } return QVariant(); }
When I load data into my table I see 3 times the message "Invalid headerData section" with indices of 15, 16 and 17, despite the fact that my header contains only 15 entries.
My table looks fine, my data works, all of my code and the application behave perfectly fine (no rendering issues, no index out of bounds, no memory violations, etc.).
So do you know why any code within Qt might query some indices that lie outside of my header data? When I swap the "ifs" so that the index is checked before the role I see 5x the number of queries so it seems that different roles are also interested in the exact same indices. Shouldn't the calling code check columnCount before querying and Index?
Thanks!
-
I found the reason, quite stupid honestly from my side! The orientation was always "vertical" so my header makes no sense since it is only for my columns (horizontal).
Here is my adapted code for headerData:
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole) { return QVariant(); } if (orientation == Qt::Orientation::Horizontal) { if (section < 0 || section >= m_Header.size()) { qWarning() << "Invalid headerData section" << section << orientation; return QVariant(); } return QVariant(m_Header[section]); } return QVariant(); }
I mark this as done : - )
-
Make a breakoint at the line, look at the backtrace.
-
I found the reason, quite stupid honestly from my side! The orientation was always "vertical" so my header makes no sense since it is only for my columns (horizontal).
Here is my adapted code for headerData:
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole) { return QVariant(); } if (orientation == Qt::Orientation::Horizontal) { if (section < 0 || section >= m_Header.size()) { qWarning() << "Invalid headerData section" << section << orientation; return QVariant(); } return QVariant(m_Header[section]); } return QVariant(); }
I mark this as done : - )