QAbstractItemModel + ModelTest::rowsInserted 断言问题

发布于 2024-12-04 20:17:50 字数 1191 浏览 1 评论 0原文

我正在尝试使用 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 最后的断言 (*)。假设我在我的应用程序中添加 1 行。 该行是存储在我的模型中的唯一行。所以行号将为 0。

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

beginInsertRows(parentIndex, 0, 0);

那么为什么 modeltest 需要

模型->数据 (模型->索引(start, 0, Parent))

等于

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

我在这里缺少什么?请帮忙:)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以为你会在 2024-12-11 20:17:50

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

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

并且在

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文