Search code examples
c++qtqt4qabstractitemmodel

QAbstractItemModel + ModelTest::rowsInserted ASSERTion problem


I'm trying to debug my model (QAbstractItemModel) with ModelTest. And I can't understand one assertion.

There are two slots in ModelTest that intercept signals generated by my model.

  1. ModelTest::rowsAboutToBeInserted
  2. ModelTest::rowsInserted

Slot/function 1 looks like this

void ModelTest::rowsAboutToBeInserted ( const QModelIndex &parent, int start, int end )
{
    Changing c;
    // ...
    c.last = model->data ( model->index ( start - 1, 0, parent ) );
    c.next = model->data ( model->index ( start, 0, parent ) );
    insert.push ( c );
}

And slot 2 looks like this

void ModelTest::rowsInserted ( const QModelIndex & parent, int start, int end )
{
    Changing c = insert.pop();

    // other asserts ...

    (*) Q_ASSERT ( c.next == model->data ( model->index ( end + 1, 0, c.parent ) ) );
}

I don't understand dla last assertion (*). Lets assume that in my app I add 1 row. This row is the only row that is stored in my model. So the row number would be 0.

In my model before adding the row I call

beginInsertRows(parentIndex, 0, 0);

So why does modeltest require

model->data ( model->index ( start, 0, parent ) )

to be equal to

model->data ( model->index ( end + 1, 0, c.parent ) )

What am I missing here ? Please help :)


Solution

  • The idea behind this assert is to check if first row after added ones was correctly moved. If there are some rows after inserted ones, then their data are compared. If there aren't any, your model should both in the line

    c.next = model->data ( model->index ( start, 0, parent ) );
    

    and in

    Q_ASSERT ( c.next == model->data ( model->index ( end + 1, 0, c.parent ) ) );
    

    should return invalid (empty) QVariant. If both return empty QVariant (as they should) assertion succedes, thus providing some level of error-checking even in case of no rows after currently inserted.