4

假设我有一个源模型,其中行运动装饰有

beginMoveRows()
endMoveRows()

(因此发出layoutChanged())。

然后,设置了一个QSortFilterProxyModel实例来过滤源模型的行。当我从源代码中得到它时,源模型布局的更改会导致代理模型的相应重新调整(因为它会监听layoutChanged()信号并正确调整自身)。

但文档只说:

注意:默认情况下,模型不会在原始模型更改时动态重新排序和重新过滤数据。可以通过设置 dynamicSortFilter 属性来更改此行为。

dynamicSortFilter : bool 此属性保存代理模型是否在源模型的内容发生变化时动态排序和过滤。

我的问题:

  • (1)“每当原始模型更改时”是否包括“在原始模型中移动行时”
  • (2) 如果是,代理模型中的项目顺序是否保证与源模型中的相同,以及
  • (3)有没有人在开发中依赖上述行为的经验?
  • (4) [额外问题]如果我尝试在 QSortFilterProxyModel 子类中跟踪源模型的行 (AboutToBe)Moved 信号并在那里调用 beginMoveRows/endMoveRows (使用适当映射的行索引),我是否正确,这会弄乱代理,因为在处理 onLayoutChanged 源模型的信号时,beginMoveRows/endMoveRows 调用将尝试调整已由 QSortFilterProxyModel 调整的持久索引?

谢谢。

4

1 回答 1

3

The problems you mentioned in your comment seem to appear only when you play with number or ordering of columns, so I think your model is unaffected.

The detailed specification as to what is intended to happen is not provided, so the only alternative is to lookup the sources and hope that it will no change in future versions. After a glance into the sources I think that answers to your questions are:

  • (1) Yes, qt is responding to layoutChanged() and layoutAboutToBeChanged() signals, updating it's indexes and emiting own layoutChanged() and layoutAboutToBeChanged(), so it's reacting to row moving.
  • (2) Yes if you don't use sorting, just filtering. Qt in such case erases entire internal mapping and re-builds it from scratch so the data will be just as they appear in source model. Sorting of course will scatter new data around. Note that if you are using sorting and moving rows within the same parent, sorted model should not show any change.
  • (3) No. I am just reading qt source and interpreting. You are free to disregard any information provided ;)
  • (4) Yes, layoutChanged() is updating persistent indexes already, so updating it again can only mess up things.
于 2012-09-03T23:09:58.197 回答