1

I have an abstractTable model with a very odd problem.

The rowCount method;

int ordersModel::rowCount(const QModelIndex &) const
{
    int i = oData->count();
    qDebug() << "rowc=" << i;

    return 711;
  //  return   oData->count();

}

Works when I hard code the value (711 or something lower) but if I use return i; or return oData->count(); the table displays nothing. (qDebug() reports that the data has 711 entries.

The headers appear correctly. I'm in Qt 5.4.1, Windows 7.

Stumped!

john elemans
  • 2,578
  • 2
  • 15
  • 26
  • 2
    Please add more information. What is `ordersModel`? Have you tried to find const 711 in other files (may it is hard coded somewhere else)? – Ilya Apr 20 '15 at 06:19
  • 2
    Hi, I think that you're looking at the problem from the wrong angle: the rowCount() method will be called by the any view at not predictable times: are you sure that the oData object or structure is properly initialized, when the method is called. Is the output you're sending to qDebug() stable? By just looking at the few lines of code you posted it's a bit difficult to help you. – pragmanomos Apr 20 '15 at 09:23
  • Does `oData` have any items when you assign it to the item view? If you add items after assigning it to the view, you must run the `beginInsertRows` and `endInsertRows` functions to force the view to recheck the data. – RobbieE Apr 20 '15 at 14:14
  • Albert set me on the right track. I still don't understand why the sequence failed, but the record count was wrong when the update started despite what rowCount returned. By adding beginResetModel and endResetModel around the data load, the table views update correctly. Many thanks Albert and Robbie (your suggestion sent me to the docs). – john elemans Apr 21 '15 at 05:47

2 Answers2

1

Try

int MyModel::rowCount(const QModelIndex &parent) const {

  if ( parent.isValid() )
      return 0;

  return m_entryList.size();
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Kombinator
  • 107
  • 6
  • 1
    This isn't merely something to try, it's a necessary step in making `rowCount` fulfill the requirements placed on it by the `QAbstractTableModel` contract. – Kuba hasn't forgotten Monica Jun 06 '17 at 13:58
  • But how shold I undestand the Qt documentation: Note: When implementing a table based model, rowCount() should return 0 when the parent is valid. – Kombinator Jun 06 '17 at 14:58
  • All I'm saying is that your answer is true, but could use stronger language: "trying" it is not enough, the implementation **must** check for `parent.isValid()` precisely as you did :) – Kuba hasn't forgotten Monica Jun 06 '17 at 17:55
0

Your problem may have nothing to do with the rowCount method itself, although even that method is not correct - see the other answer. Other aspects of the model's implementation likely don't fulfill the basic contract that every model must fulfill.

The following sequence of events takes place:

  1. Your model starts with no elements.
  2. A view is attached to the model. The view expects the model to fulfill the requirements of a QAbstractItemModel.
  3. You add data to the model. The model fails to emit the rowsInserted signal - it doesn't call beginInsertRows and endInsertRows. It has broken its contract.
  4. You've now triggered undefined behavior in the view and Things Don't Work™.

See the first section of this answer about model semantics, and make sure you understand it and follow the requirements.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313