2

我正在尝试使用 ModelTest 调试我的模型(QAbstractItemModel)。我无法理解一个断言。

ModelTest 中有两个插槽可以拦截我的模型生成的信号。

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

插槽/功能 1 看起来像这样

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 );
}

插槽 2 看起来像这样

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 ) ) );
}

我不明白 dla last 断言 (*)。让我们假设在我的应用程序中我添加了 1 行。此行是存储在我的模型中的唯一行。所以行号为0。

在我的模型中添加我调用的行之前

beginInsertRows(parentIndex, 0, 0);

那么为什么modeltest需要

模型->数据(模型->索引(开始,0,父))

等于

模型->数据(模型->索引(end + 1, 0, c.parent))

我在这里想念什么?请帮忙 :)

4

1 回答 1

1

这个断言背后的想法是检查添加后的第一行是否正确移动。如果插入的行之后还有一些行,则比较它们的数据。如果没有,您的模型应该都在行中

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

并且在

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

应该返回无效(空)QVariant。如果两者都返回空的 QVariant(它们应该),则断言成功,因此即使在当前插入后没有行的情况下也提供一定程度的错误检查。

于 2011-09-15T23:46:31.860 回答