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. Why is my table model (based on QAbstractTableModel) asked for header section (index) that is out of range?
Forum Updated to NodeBB v4.3 + New Features

Why is my table model (based on QAbstractTableModel) asked for header section (index) that is out of range?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 496 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.
  • F Offline
    F Offline
    FrozenTarzan
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • F Offline
      F Offline
      FrozenTarzan
      wrote on last edited by
      #3

      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 : - )

      1 Reply Last reply
      2
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Make a breakoint at the line, look at the backtrace.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2
        • F Offline
          F Offline
          FrozenTarzan
          wrote on last edited by
          #3

          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 : - )

          1 Reply Last reply
          2

          • Login

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