简体   繁体   中英

How to deselect all items in QTreeWidget?

I have tried every suggestion I can come across online, from setting flags to using selectionModel

def DatabaseLoadWrapper(self,database, init):
    self.treeWidget.currentItemChanged.disconnect(self.updateStackedWidget)
    self.DatabaseLoad(database, init)
    self.treeWidget.clearSelection()
    self.treeWidget.setCurrentItem(self.treeWidget.findItems(self.selectedDatabase,Qt.MatchExactly|Qt.MatchRecursive)[0])
    self.treeWidget.currentItemChanged.connect(self.updateStackedWidget)

This is where my code needs to force a selection on the QTreeWidget, none of the code I use throws up any errors but also has no effect on the selection. And I end up with this where the user has selected Database 1 but I need to revert back to having only Database 2 selected:

例


Edit: The Tree Widget is built using this code:

def setupMenu(self):
    self.DatabaseParent = QTreeWidgetItem(['Databases'])
    for item in NamesInDatabase():
        self.DatabaseParent.addChild(QTreeWidgetItem([item]))
    self.AverageParent = QTreeWidgetItem(['Averaged Database'])
    self.SortingParent = QTreeWidgetItem(['Waste Composition'])
    self.ResultParent = QTreeWidgetItem(['Results'])
    self.treeWidget.addTopLevelItem(self.DatabaseParent)
    self.treeWidget.addTopLevelItem(self.AverageParent)
    self.treeWidget.addTopLevelItem(self.SortingParent)
    self.treeWidget.addTopLevelItem(self.ResultParent)

It basically is adding databases, averaged database, waste compisition & results, as fixed parts of the navigation menu and then populating children of databases with the names of the databases in the save file.

Your question fails to expose the part of the code that is causing the problem. By default, setting the current item, as you do, also sets the selection. So this code, for example, correctly sets the selection to item "b":

from PySide import QtCore,QtGui

if __name__ == '__main__':
    import sys
    qApp = QtGui.QApplication(sys.argv)
    treeWidget = QtGui.QTreeWidget()
    parent = QtGui.QTreeWidgetItem(['Databases'])
    items = []
    for item_text in ["a","b","c"]:
        item = QtGui.QTreeWidgetItem([item_text])
        items.append(item)
        parent.addChild(item)
    treeWidget.addTopLevelItem(parent)

    treeWidget.setCurrentItem(items[1])

    treeWidget.show()
    sys.exit(qApp.exec_())

However, I suspect there is code elsewhere in your project that is affecting this. For example, if you had set the selection mode for the QTableWidget selection model to MultiSelection then selections become cumulative:

from PySide import QtCore,QtGui

if __name__ == '__main__':
    import sys
    qApp = QtGui.QApplication(sys.argv)
    treeWidget = QtGui.QTreeWidget()
    parent = QtGui.QTreeWidgetItem(['Databases'])
    items = []
    for item_text in ["a","b","c"]:
        item = QtGui.QTreeWidgetItem([item_text])
        items.append(item)
        parent.addChild(item)
    treeWidget.addTopLevelItem(parent)

    treeWidget.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)

    treeWidget.setCurrentItem(items[0])
    treeWidget.setCurrentItem(items[2])

    treeWidget.show()

    sys.exit(qApp.exec_())

However, that still doesn't explain your issue because the clearSelection call should have cleared the preceding selection in any case. Further debugging of your code is needed, for example to check that the wrapper function and the setCurrentItem are being called as you claim. Also check what else is being called subsequent to the DatabaseLoadWrapper .

在Pyside2中,这对我有用:如果单击treewidget,则选择将被清除。

self.treeWidget.setSelectionMode(QtWidgets.QAbstractItemView.ContiguousSelection)

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