简体   繁体   English

PyQt:获取当前的QTreeWidget项

[英]PyQt: get the current QTreeWidget item

I presently have a QTreeWidget named 'treeWidget', and for the life of me, cannot figure out how to get the index value or the text of the selected treeWidget branch. 我现在有一个名为'treeWidget'的QTreeWidget,对于我的生活,无法弄清楚如何获取索引值或所选treeWidget分支的文本。

self.treeWidget looks like: self.treeWidget看起来像:
User 用户
-Inbox -inbox
-Sent -Sent
-Drafts -Drafts
-Trash -垃圾

I need to know which branch is selected so I can display folders in the branch's corresponding file folder. 我需要知道选择了哪个分支,这样我才能在分支的相应文件夹中显示文件夹。 I've been trying to understand the Qt documentation, but I'm totally stumped by the C++. 我一直在努力理解Qt文档,但我完全被C ++困扰了。 And the PyQt docs don't have any examples. 并且PyQt文档没有任何示例。 I've searched everywhere for three days trying to tinker and figure out the answer but keep coming up with errors. 我在各处搜索了三天,试图修补并找出答案,但不断出现错误。

The closest I think I've come is something like this: 我认为我最接近的是这样的:

self.connect(self.treeWidget,SIGNAL("itemSelectionChanged()"), self.loadAllMessages)

def loadAllMessages(self, folder):
    item = self.treeWidget.currentItem()

Do I need to setSelectionMode first or something? 我需要首先设置setSelectionMode吗? All help is greatly appreciated! 非常感谢所有帮助!

Try This 尝试这个

#remove the old way of connecting
#self.connect(self.treeWidget,SIGNAL("itemSelectionChanged()"), self.loadAllMessages)
self.treeWidget.itemSelectionChanged.connect(self.loadAllMessages)
def loadAllMessages(self, folder):
    getSelected = self.treeWidget.selectedItems()
    if getSelected:
        baseNode = getSelected[0]
        getChildNode = baseNode.text(0)
        print getChildNode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM