从TreeView中选择了哪个节点(PySide)?

2024-04-26 23:20:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个QTreeView程序,我想禁用/启用工具栏中的某些操作,这取决于选择了哪个节点,但我不知道如何获得选定的节点。有人能帮忙吗?你知道吗


Tags: 程序节点工具栏qtreeview
1条回答
网友
1楼 · 发布于 2024-04-26 23:20:35

在下面的示例中,我展示了如何知道在QTreeView中选择了哪些项,为此,我们使用selectionChanged信号返回选中和取消选中的项,然后迭代并获得QModelIndex,并通过这个和模型获得数据。你知道吗

from PySide.QtGui import *
from PySide.QtCore import *

class Main(QTreeView):
    def __init__(self):
        QTreeView.__init__(self)
        model = QFileSystemModel()
        model.setRootPath(QDir.homePath())
        self.setModel(model)
        m = self.selectionModel()
        m.selectionChanged.connect(self.onSelectionChanged)

    def onSelectionChanged(self, selected, deselected):
        for index in selected.indexes():
            print(self.model().data(index))


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = Main()
    w.show()
    sys.exit(app.exec_())

相关问题 更多 >