QAbstractItemModel + ModelTest::rowsInserted ASSERTion problem

白昼怎懂夜的黑 提交于 2019-12-08 02:06:01

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/7438539/qabstractitemmodel-modeltestrowsinserted-assertion-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!