我可以为 QAbstractItemModel 声明根项吗?

发布于 2024-08-05 18:25:39 字数 307 浏览 3 评论 0原文

我知道可以为项目视图设置父模型索引,但我想为模型执行此操作。

原因很简单,我想摆脱这些构造:

# 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 技术交流群。

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

发布评论

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

评论(1

撩发小公举 2024-08-12 18:25:39

如果您的模型表示平面结构,则只需忽略父属性或在必须指定父级的任何地方使用 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.

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