2017-07-19 36 views
0

複数の選択肢のフォルダを取得したいと思います。私はクリックすると強調表示されているものだけを取得したい。理想的には、リストはインタラクティブに更新されます。つまり、あるフォルダが選択解除されていると、自動的にリストから削除されます。QTreeView:強調表示されている項目のリストを取得する(複数選択)

ここではQTreeViewの例を示します...サポートしてください。

はあなたがQItemSelectionModelselectedIndexes()方法を使用することができます

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_()) 

答えて

1

ありがとうございます。

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_()) 
+0

いいね!ありがとうございました。 :) – user3046026

関連する問題