简体   繁体   English

如何取消选择QTreeWidget中的所有项目?

[英]How to deselect all items in QTreeWidget?

I have tried every suggestion I can come across online, from setting flags to using selectionModel 从设置标志到使用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. 这是我的代码需要在QTreeWidget上强制选择的地方,我使用的任何代码都不会引发任何错误,但也不会影响选择。 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: 最后,用户选择了数据库1,但是我需要恢复为仅选择数据库2:

例


Edit: The Tree Widget is built using this code: 编辑:Tree Widget是使用以下代码构建的:

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": 因此,例如,此代码正确地将选择设置为项目“ 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: 例如,如果您已将QTableWidget选择模型的选择模式设置为MultiSelection,则选择将变为累积式:

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. 但是,这仍然不能解释您的问题,因为无论如何, clearSelection调用应该已经清除了先前的选择。 Further debugging of your code is needed, for example to check that the wrapper function and the setCurrentItem are being called as you claim. 需要对代码进行进一步的调试,例如,以检查包装函数和setCurrentItem是否如您所要求的那样被调用。 Also check what else is being called subsequent to the DatabaseLoadWrapper . 还要检查DatabaseLoadWrapper之后还调用了什么。

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

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

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

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