Sorry for the redirection but I've had some problem with my mail clients, and I had disabled the list mails for a while...

Ooooopssss!!! the first parameter is the milliseconds!! I thought it was the seconds!! Now it is working correctly! ( I know I should have looked the docs...) I'll attach the file browser class to this mail. The problem is at row 34, the setFilter method. I want to display only regular files in left widget, but the filter displays all
the files and directory even the hidden ones!
If you comment out that line everything is working but you get files and folders in the left widget!
Thanks

Gionata Boccalini


Il 23/11/2010 14:46, Hans-Peter Jansen ha scritto:
[Rebounce to list, no need to address me directly..]

On Tuesday 23 November 2010, 14:11:17 Gionata Boccalini wrote:
Ok, I now I have

QTimer.singleShot(4, self.resizeColumn)

in the slot, where self.resizeColumn is a callable:

   def resizeColumn(self):
          self.treeView.resizeColumnToContents(0)
          self.fileView.resizeColumnToContents(0)

Is there some way to call directly the resizeColumnToContents from
the Qtimer without using a slot???
You can use lambdas, but what's your problem with slots?

But this solution is working only sometimes, and if I set the wait to
be less than 4 seconds it works even worst... any idea??
In fact, these are 4 milliseconds. Please explain "even worst".

And I also have another problem, that is not related to the resize
column I think: when I set the filter on the model to show only files
with

   self.fileModel.setFilter(QDir.Files)

the result is a list of files, dirs, even hidden files, without any
ordering...
Please be patient with me ;) I am still a beginner.... thanks!!
This gives you even more reasons to provide a minimum runnable example
demonstrating your issues.

Note this this practice is the single most successful procedure to solve
complex [PyQt] problems, even for advanced practitioners.
Hence, in doing so, you are in good company.

Pete

Gionata Boccalini

On Monday 22 November 2010, 20:56:35 Gionata Boccalini wrote:
/  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...
/

I remember having similar issues, that I was able to workaround with
delaying the calls to resizeColumnToContents with
QTimer.singleShot(0, ...)

Might be worth a try..

Pete

import sys

from PyQt4 import QtGui,  QtCore
from PyQt4.QtCore import QDir, Qt, QFileInfo, QTimer
from PyQt4.QtGui import QSplitter


class FileBrowser(QSplitter):
    def __init__(self, parent = None):
        super(FileBrowser, self).__init__(parent)

        self.setOrientation(Qt.Horizontal)

        # Directory tree and model
        self.__treeView = QtGui.QTreeView()
        self.__treeView.setSortingEnabled (True)
        self.__dirModel = QtGui.QFileSystemModel()
        self.__dirModel.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot)
        # Path
        rootindex = self.__dirModel.setRootPath(QDir.rootPath())
        homeindex = self.__dirModel.index(QDir.homePath())
        self.__treeView.setModel(self.__dirModel)
        self.__treeView.setRootIndex(rootindex)
        self.__treeView.scrollTo(homeindex,  QtGui.QAbstractItemView.PositionAtTop)
        self.__treeView.expand(homeindex)
        self.__treeView.sortByColumn(0, Qt.AscendingOrder)
        self.__treeView.setAlternatingRowColors(True)
        self.__treeView.setAnimated(True)
        self.__treeView.hideColumn(1)
        self.__treeView.hideColumn(2)

        # File tree and model
        self.__fileModel = QtGui.QFileSystemModel()
        #self.__fileModel.setFilter(QDir.Files)
        self.__fileView = QtGui.QTreeView()
        self.__fileView.setModel(self.__fileModel)
        # Path
        self.__fileModel.setRootPath(QDir.rootPath())
        homeindex = self.__fileModel.index(QDir.homePath())
        self.__fileView.setRootIndex(homeindex)
        self.__fileView.setSortingEnabled (True)
        self.__fileView.sortByColumn(2, Qt.AscendingOrder)
        self.__fileView.setAlternatingRowColors(True)
        self.__fileView.setAnimated(True)
        self.__fileView.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
        QTimer.singleShot(200, self.resizeColumn)

        # Splitter layout
        self.addWidget(self.__treeView)
        self.addWidget(self.__fileView)

        # Signals
        self.__treeView.clicked.connect(self.updateFiles)
        self.__treeView.expanded.connect(self.updateFiles)
        self.__fileView.clicked.connect(self.resizeColumn)
        self.__fileView.expanded.connect(self.resizeColumn)
        self.__fileView.selectionModel().selectionChanged.connect(self.updateSelection)


    @QtCore.pyqtSlot('QModelIndex')
    def updateFiles(self,  index):
        path = self.__dirModel.filePath(index)
        fileIndex = self.__fileModel.index(path)
        self.__fileView.setRootIndex(fileIndex)
        QTimer.singleShot(400, self.resizeColumn)


    @QtCore.pyqtSlot()
    def updateSelection(self):
        list = self.__fileView.selectionModel().selectedIndexes()
        row = -1
        for index in list:
            if index.row() != row and index.column() == 0:
                fileInfo = QFileInfo(self.__fileModel.fileInfo(index))
                print fileInfo.fileName(),
                row = index.row()
        print


    def resizeColumn(self):
        self.__treeView.resizeColumnToContents(0)
        self.__fileView.resizeColumnToContents(0)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    MainWindowClass = QtGui.QMainWindow()
    fileb = FileBrowser()
    fileb.resize(1000, 600)
    fileb.show()
    sys.exit(app.exec_())
_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to