Important: Please read the Qt Code of Conduct -

qabstractitemmodeltester and editabletreemodel test failures

  • In qabstractitemmodeltester, the following test case is run when used on a tree model.
    qabstractitemmodeltester - SNIPPET 1:

    // Common error test #3, the second column should NOT have the same children
      // as the first column in a row.
      // Usually the second column shouldn't have children.
      if (model->hasIndex(0, 1)) {
          QModelIndex topIndex1 = model->index(0, 1, QModelIndex());
          if (model->hasChildren(topIndex) && model->hasChildren(topIndex1)) {
              QModelIndex childIndex = model->index(0, 0, topIndex);
              QModelIndex childIndex1 = model->index(0, 0, topIndex1);
              MODELTESTER_VERIFY(childIndex != childIndex1);

    As per the editableTreeModel example, if the column is anything other than 0, the model returns an invalid index in the index method as shown below:
    editableTreeModel - SNIPPET 2:

    if (parent.isValid() && parent.column() != 0)
          return QModelIndex();

    Going back to the tester code above:
    In SNIPPET 1, childIndex1 will be invalid, because SNIPPET 2 returns an invalid index for all columns other than zero.
    If I remove the column 0 check in SNIPPET 2, then in SNIPPET 1 it fails on the last line because childIndex IS EQUAL TO childIndex.

    What is the right implementation here? Only when I return an index for all columns, the code seems to work correctly, but the model tester fails..

  • Lifetime Qt Champion

    So what's your actual implementation? The tester is correct. If the model says it has an index at index(x/y) then it should return a unique and valid one.

  • @Christian-Ehrlicher Would it be possible for you to look at the editabletreemodel example that ships with QtCreator? When the model tester is applied to the example, it will fail with the failures above.

    Here are the steps to apply the modeltester to the editabletreemodel example:
    1)after the editabletreemodel example has been setup in QtCreator, open the file. Add "testlib" to the this line "QT += widgets ".
    2)Open mainwindow.cpp and add ```
    "#include <QAbstractItemModelTester>"
    3) Add the line shown below

    //The line below is in the example
    TreeModel *model = new TreeModel(headers, file.readAll());
    //Add the line below
    new QAbstractItemModelTester(model, QAbstractItemModelTester::FailureReportingMode::Fatal, this);

    4)Run the example and you will see the above failures.


  • Lifetime Qt Champion

    @JohnGa Thx, the columnCount() function is wrong (as already guessed in my first post). It must be
    int TreeModel::columnCount(const QModelIndex &parent) const
    return parent.isValid() ? 0 : rootItem->columnCount();


    int TreeModel::columnCount(const QModelIndex &parent) const
        return (!parent.isValid() || parent.column() == 0) ? rootItem->columnCount() : 0;

    -> only the first column has sub-columns, not the second (which would look somehow strange)

    It has no real-world impact in the Qt since the current implementation does first check for other things but is wrong when you want to catch all cases - the model tester is very picky here :). Will create a patch for it, thx.

Log in to reply