Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python QTreeView:仅获取高亮显示的项目列表(多选)_Python_Pyqt_Qtreeview_Multipleselection_Getdirectories - Fatal编程技术网

Python QTreeView:仅获取高亮显示的项目列表(多选)

Python QTreeView:仅获取高亮显示的项目列表(多选),python,pyqt,qtreeview,multipleselection,getdirectories,Python,Pyqt,Qtreeview,Multipleselection,Getdirectories,我想获得多个选择的文件夹路径。我只希望在单击时获得突出显示的内容。理想情况下,列表将以交互方式更新。我的意思是,如果取消选择一个文件夹,它将自动从列表中删除 下面是QTreeView的一个示例。。。你能提供支持吗 多谢各位 from PyQt4 import QtCore, QtGui class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.resize(1150, 905) self.gri

我想获得多个选择的文件夹路径。我只希望在单击时获得突出显示的内容。理想情况下,列表将以交互方式更新。我的意思是,如果取消选择一个文件夹,它将自动从列表中删除

下面是QTreeView的一个示例。。。你能提供支持吗

多谢各位

from PyQt4 import QtCore, QtGui

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.resize(1150, 905)
        self.gridLayout_2 = QtGui.QGridLayout(Dialog)
        self.groupBox     = QtGui.QGroupBox(Dialog)
        self.gridLayout   = QtGui.QGridLayout(self.groupBox)
        self.treeView     = QtGui.QTreeView(self.groupBox)
        self.gridLayout.addWidget(self.treeView, 0, 0, 1, 1)
        self.gridLayout_2.addWidget(self.groupBox, 0, 0, 1, 2)
        self.fileSystemModel = QtGui.QFileSystemModel(self.treeView)
        self.fileSystemModel.setFilter(QtCore.QDir.AllDirs | QtCore.QDir.NoDotAndDotDot | QtCore.QDir.AllEntries)
        self.fileSystemModel.setReadOnly(True)
        self.root = self.fileSystemModel.setRootPath('/home/')
        self.treeView.setModel(self.fileSystemModel)
        self.treeView.setRootIndex(self.root)
        self.treeView.setSelectionMode(QtGui.QAbstractItemView.ContiguousSelection)   

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

您可以使用的
selectedIndex()
方法


您可以使用的
selectedIndex()
方法

from PyQt4 import QtCore, QtGui

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.resize(1150, 905)
        self.gridLayout_2 = QtGui.QGridLayout(Dialog)
        self.groupBox     = QtGui.QGroupBox(Dialog)
        self.gridLayout   = QtGui.QGridLayout(self.groupBox)
        self.treeView     = QtGui.QTreeView(self.groupBox)
        self.gridLayout.addWidget(self.treeView, 0, 0, 1, 1)
        self.gridLayout_2.addWidget(self.groupBox, 0, 0, 1, 2)
        self.fileSystemModel = QtGui.QFileSystemModel(self.treeView)
        self.fileSystemModel.setFilter(QtCore.QDir.AllDirs | 
            QtCore.QDir.NoDotAndDotDot | QtCore.QDir.AllEntries)
        self.fileSystemModel.setReadOnly(True)
        self.root = self.fileSystemModel.setRootPath('/home/')
        self.treeView.setModel(self.fileSystemModel)
        self.treeView.setRootIndex(self.root)
        self.treeView.setSelectionMode(QtGui.QAbstractItemView.ContiguousSelection)
        self.treeView.selectionModel().selectionChanged.connect(self.getItems)


    def getItems(self):
        selected = self.treeView.selectionModel().selectedIndexes()
        for index in selected:
            if index.column() == 0:
                print self.fileSystemModel.data(index,
                    self.fileSystemModel.FileNameRole).toString()

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())