我可以为 QAbstractItemModel 声明根项吗?
我知道可以为项目视图设置父模型索引,但我想为模型执行此操作。
原因很简单,我想摆脱这些构造:
# PyQt 4.5.4, but this should be similar to Qt.
def insertRows(self, row, count, parent=QModelIndex()):
if parent.isValid():
parentNode = parent.internalPointer()
else:
parentNode = self.rootNode
I know there is the possibility of setting the parent model index for the item view, but I'd like to do this for the model.
The reason is quite simple, I want to get rid of those constructs:
# PyQt 4.5.4, but this should be similar to Qt.
def insertRows(self, row, count, parent=QModelIndex()):
if parent.isValid():
parentNode = parent.internalPointer()
else:
parentNode = self.rootNode
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的模型表示平面结构,则只需忽略父属性或在必须指定父级的任何地方使用 QModelIndex() 即可。如果您的模型代表树,您无法真正摆脱您发布的构造,因为有人可能使用无效索引,您必须检查它是否有效。
If your model represents flat structure, just ignore parent attribute or use QModelIndex() everywhere you have to specify parent. If your model represents the tree, you can't really get rid of the constructs you posted, because someone may use invalid index and you have to check if it's valid.