Python: PyQt QTreeview example - selection

I use Python 2.7 and the Qt constructor, and I'm new to MVC: I have a view completed in Qt to give me a list of the directory tree and a controller in place to run. My question is:

Given the Qtree view, how can I get the directory after selecting dir?

enter image description here

A snapshot of the code below, I suspect this is a SIGNAL (..), although I'm not sure:

class Main(QtGui.QMainWindow): plot = pyqtSignal() def __init__(self): QtGui.QMainWindow.__init__(self) self.ui = Ui_MainWindow() self.ui.setupUi(self) # create model model = QtGui.QFileSystemModel() model.setRootPath( QtCore.QDir.currentPath() ) # set the model self.ui.treeView.setModel(model) **QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked()'), self.test)** def test(self): print "hello!" 
+8
python qt tree qtreeview
source share
4 answers

The signal you are looking for is selectionChanged , labeled selectionModel , belonging to your tree. This signal is replaced with the selected element as the first argument and canceled as the second, both are instances of QItemSelection .

So you can change the line:

 QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked()'), self.test) 

to

 QtCore.QObject.connect(self.ui.treeView.selectionModel(), QtCore.SIGNAL('selectionChanged()'), self.test) 

I also recommend that you use the new style for signals and slots . Override the test function as:

  @QtCore.pyqtSlot("QItemSelection, QItemSelection") def test(self, selected, deselected): print("hello!") print(selected) print(deselected) 

Here you have a working example:

 from PyQt4 import QtGui from PyQt4 import QtCore class Main(QtGui.QTreeView): def __init__(self): QtGui.QTreeView.__init__(self) model = QtGui.QFileSystemModel() model.setRootPath( QtCore.QDir.currentPath() ) self.setModel(model) QtCore.QObject.connect(self.selectionModel(), QtCore.SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self.test) @QtCore.pyqtSlot("QItemSelection, QItemSelection") def test(self, selected, deselected): print("hello!") print(selected) print(deselected) if __name__ == '__main__': import sys app = QtGui.QApplication(sys.argv) w = Main() w.show() sys.exit(app.exec_()) 

PyQt5

PyQt5 is slightly different (thanks to Carel and saldenisov for comments and aswer.)

... connect went from an object method to a method that acts on an attribute when PyQt moved from 4 to 5

Therefore, instead, it is known:

 QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked()'), self.test) 

now you write:

 class Main(QTreeView): def __init__(self): # ... self.setModel(model) self.doubleClicked.connect(self.test) # Note that the the signal is now a attribute of the widget. 

Here is an example (by saldenisov) using PyQt5.

 from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication class Main(QTreeView): def __init__(self): QTreeView.__init__(self) model = QFileSystemModel() model.setRootPath('C:\\') self.setModel(model) self.doubleClicked.connect(self.test) def test(self, signal): file_path=self.model().filePath(signal) print(file_path) if __name__ == '__main__': import sys app = QApplication(sys.argv) w = Main() w.show() sys.exit(app.exec_()) 
+12
source share

In PyQt5, this can be done as follows:

 from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication class Main(QTreeView): def __init__(self): QTreeView.__init__(self) model = QFileSystemModel() model.setRootPath('C:\\') self.setModel(model) self.doubleClicked.connect(self.test) def test(self, signal): file_path=self.model().filePath(signal) print(file_path) if __name__ == '__main__': import sys app = QApplication(sys.argv) w = Main() w.show() sys.exit(app.exec_()) 
+2
source share

I tried this alternative to get the file name ...

Instead:

 indexItem = self.treeview.model.index(index.row(), 0, index.parent()) # path or filename selected fileName = self.treeview.model.fileName(indexItem) 

I tried:

 # path or filename selected fileName = index.internalPointer().fileName 

Which also works ...

+1
source share

If I understood the question correctly, you would like the directory or file name to be selected.

This is what I do:

 from PyQt4 import QtGui from PyQt4 import QtCore # --------------------------------------------------------------------- class MainWindow(QtGui.QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.resize(600,400) self.setWindowTitle("Treeview Example") self.treeview = QtGui.QTreeView(self) self.treeview.model = QtGui.QFileSystemModel() self.treeview.model.setRootPath( QtCore.QDir.currentPath() ) self.treeview.setModel(self.treeview.model) self.treeview.setColumnWidth(0, 200) self.setCentralWidget(self.treeview) self.treeview.clicked.connect(self.on_treeview_clicked) # --------------------------------------------------------------------- @QtCore.pyqtSlot(QtCore.QModelIndex) def on_treeview_clicked(self, index): indexItem = self.treeview.model.index(index.row(), 0, index.parent()) # path or filename selected fileName = self.treeview.model.fileName(indexItem) # full path/filename selected filePath = self.treeview.model.filePath(indexItem) print(fileName) print(filePath) # --------------------------------------------------------------------- if __name__ == '__main__': import sys app = QtGui.QApplication(sys.argv) w = MainWindow() w.show() sys.exit(app.exec_()) 
0
source share

All Articles