Mic*_*iak 6 model-view-controller qt qt5
我正在使用Qt5编写一个程序,它可以在Linux上运行正常但在Windows上我观察到了奇怪的行为:
当QTreeView::setModel被调用时,它询问了index(QAbstractItemModel::index)的模型,其中包含一些row和col以及无效的parent.它从未happend在Linux上,鉴于总是问hasChildren,rowCount之前调用等index.
我已经下载了Qt5的来源,看看发生了什么,我可以看到:
// These asserts do basic sanity checking of the model
Q_ASSERT_X(d->model->index(0,0) == d->model->index(0,0),
"QAbstractItemView::setModel",
"A model should return the exact same index "
"(including its internal id/pointer) when asked for it twice in a row.");
Q_ASSERT_X(!d->model->index(0,0).parent().isValid(),
"QAbstractItemView::setModel",
"The parent of a top level index should be invalid");
Run Code Online (Sandbox Code Playgroud)
我无法在视图类和模型类的文档中找到关于那些健全性检查的单词.
他们在哪里定义?
另一个有趣的事情是,我可以通过观察模型/视图类来推断我写的顶级索引应该是无效的但我无法直接在文档中找到这些信息.
来自以下文档QAbstractItemModel::parent():
如果该项没有父项,则返回无效的 QModelIndex。
这意味着使用index()无效的 QModelIndex 进行调用会要求顶级项目。
您遇到的健全性检查可能已在 Linux 中被禁用(也许是发布版本?) - 但您的模型的功能决不应该依赖于函数调用的顺序。
如果index()使用无效的行/列参数调用(如果您的模型尚未填充),则返回QModelIndex()。
| 归档时间: |
|
| 查看次数: |
1169 次 |
| 最近记录: |