[PyQt] Extended Dir View sample

kib2 kib2 at free.fr
Mon Nov 5 16:01:35 GMT 2007


Someone posted on the Qt Wiki an Extended Dir View sample today,
I've tried to convert it to PyQt4, so here it is :

 >-----------------------------------------------------------------
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# ExtendedDirView sample
# from QtWiki :
# http://wiki.qtcentre.org/index.php?title=Extended_Dir_View_example

import sys
from PyQt4 import QtGui, QtCore

class ExtendedDirView(QtGui.QMainWindow):
     def __init__(self):
         QtGui.QMainWindow.__init__(self)

         self.model = QtGui.QDirModel(self)
         self.model.setReadOnly(False)

         self.tree = QtGui.QTreeView()
         self.tree.setModel(self.model)
         self.tree.setDragEnabled(True)
         self.tree.setAcceptDrops(True)

         self.list = QtGui.QListView()
         self.list.setModel(self.model)
         self.list.setDragEnabled(True)
         self.list.setAcceptDrops(True)

         self.splitter = QtGui.QSplitter(self)
         self.splitter.addWidget(self.tree)
         self.splitter.addWidget(self.list)
         self.setCentralWidget(self.splitter)

         self.history = []
         self.future = []

         QtCore.QObject.connect(self.tree, 
QtCore.SIGNAL("clicked(QModelIndex)"),
                             self.setRootIndex)
         QtCore.QObject.connect(self.list, 
QtCore.SIGNAL("doubleClicked(QModelIndex)"),
                             self.setRootIndex)

         self.toolBar = self.addToolBar(self.tr("&Location Toolbar"))
         self.backAction    = self.toolBar.addAction(self.tr("&Back"), 
self.goBack)
         self.forwardAction = 
self.toolBar.addAction(self.tr("&Forward"), self.goForward)
         self.upAction      = self.toolBar.addAction(self.tr("&Up"), 
self.goUp)
         self.homeAction    = self.toolBar.addAction(self.tr("&Home"), 
self.goHome)
         self.refreshAction = 
self.toolBar.addAction(self.tr("&Refresh"), self.refresh)

         self.currentPath = QtCore.QDir.homePath()
         self.setRootIndex(self.model.index(self.currentPath), False)

     def goBack(self):
         self.future.append(self.currentPath)
         currentPath = self.history.pop()
         self.setRootIndex(self.model.index(self.currentPath), False)

     def goForward(self):
         self.history.append(self.currentPath)
         try:
             self.currentPath = self.future.pop()
         except:
             pass
         self.setRootIndex(self.model.index(self.currentPath), False)

     def goUp(self):
         root = self.list.rootIndex()
         self.setRootIndex(root.parent())

     def goHome(self):
         path = QtCore.QDir.homePath()
         self.setRootIndex(self.model.index(path))

     def refresh(self):
         self.model.refresh(self.list.rootIndex())

     def setRootIndex(self, index, remember = True):
         if index != self.list.rootIndex() and 
self.model.fileInfo(index).isDir():
             if remember:
                 self.future = []
                 self.history.append(self.currentPath)
                 currentPath = self.model.filePath(index)
             self.list.setRootIndex(index)
             self.tree.setCurrentIndex(index)
         self.updateActions()

     def updateActions(self):
         if len(self.history) > 0 :
             self.backAction.setEnabled(True)
         if len(self.future) > 0 :
             self.forwardAction.setEnabled(True)
         self.upAction.setEnabled(self.list.rootIndex().isValid())

if __name__ == "__main__":
     app = QtGui.QApplication(sys.argv)
     window = ExtendedDirView()
     window.show()
     sys.exit(app.exec_())
 >-----------------------------------------------------------------

If you see any errors, please tell me.
Cheers,

Christophe K.


More information about the PyQt mailing list