简体   繁体   中英

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.

self.treeWidget looks like:
User
-Inbox
-Sent
-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++. And the PyQt docs don't have any examples. 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? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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