Linux 拨号vps windows公众号手机端

qtreeview怎么获取根节点

lewis 8年前 (2017-08-07) 阅读数 8 #程序编程
文章标签 qtreeview

在QTreeView中,可以使用model()方法获取treeview所使用的model。然后使用model的index()方法获取根节点的index,再使用model的parent()方法获取该节点的父节点的index,直到获取到没有父节点的根节点。

示例代码如下:

root_index = treeview.model().index(0, 0)  # 获取第一个节点的index
while treeview.model().parent(root_index).isValid():
    root_index = treeview.model().parent(root_index)  # 获取父节点的index
root_node = root_index.internalPointer()  # 获取根节点

请注意,上述示例代码中的0, 0表示根节点的位置,具体位置可以根据实际情况进行调整。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门