Hi, I am doing a small file browser with 2 QTreeView (treeView and fileView) linked with 2 different QFileSystemModel (dirModel and fileModel): when the user selects a folder on the left view I want the contents of that folder to be displayed in the right view, (only the files are shown in the right view while only folder are shown in the left view...).
This is the constructor method, in short:

class FileBrowser(QWidget):
    def __init__(self, parent = None):
        super(FileBrowser, self).__init__(parent)
        self.treeView = QtGui.QTreeView()
        self.treeView.setSortingEnabled (True)
        self.dirModel = QtGui.QFileSystemModel()
        self.dirModel.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot)
        self.treeView.setModel(self.dirModel)

        # Path
        rootindex = self.dirModel.setRootPath(QDir.rootPath())
        homeindex = self.dirModel.index(QDir.homePath())

        self.treeView.setRootIndex(rootindex)
self.treeView.scrollTo(homeindex, QtGui.QAbstractItemView.PositionAtTop)
        self.treeView.expand(homeindex)
        self.treeView.resizeColumnToContents(0)
        self.treeView.sortByColumn(0, Qt.AscendingOrder)

sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.treeView.setSizePolicy(sizePolicy)
        self.treeView.setAlternatingRowColors(True)
        self.treeView.setAnimated(True)
        self.treeView.hideColumn(1)
        self.treeView.hideColumn(2)

        ....
        self.fileModel = QtGui.QFileSystemModel()
        #self.fileModel.setFilter(QDir.Files)
        self.fileView = QtGui.QTreeView()
        self.fileView.setModel(self.fileModel)
        self.fileModel.setRootPath(QDir.rootPath())
        homeindex = self.fileModel.index(QDir.homePath())

        .......
        ....

        self.fileView.setRootIndex(homeindex)
        self.fileView.setSizePolicy(sizePolicy)
        self.fileView.setSortingEnabled (True)
        self.fileView.sortByColumn(2, Qt.AscendingOrder)
        self.fileView.setAlternatingRowColors(True)
        self.fileView.setAnimated(True)

        # Signals
        self.treeView.clicked.connect(self.updateFiles)
        ...
        ...



The problem is the resizeColumnToContents(0) method of QtreeView, which is called in a slot on the "clicked" QTreeView signal: when I click on the folder in the left view the first column is resized, but the first column of the right view is resized only on the second click on the same folder (on the left view.... quite tricky to explain....)!!!
This is the code I am using in the slot for the clicked() signal:

 @QtCore.pyqtSlot('QModelIndex')
    def updateFiles(self,  index):
        path = self.dirModel.filePath(index)
        fileIndex = self.fileModel.index(path)
        self.fileView.setRootIndex(fileIndex)

        self.treeView.resizeColumnToContents(0)
        self.fileView.resizeColumnToContents(0)


The first column shrinks only on the second click, so on the second signal... If I comment out the line self.fileView.setRootIndex(fileIndex) the resize goes as it should, but the treeView is not updated with the new files.. Maybe there is a better way to do it all, but I haven't found a good example on the net, so I am trying to do that myself. Anyway I hope I've explained it well...
Thanks for your help, and you time!!!!

--
Gionata Boccalini

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to