[PyQt] Resize columns in QTreeViewv

Hans-Peter Jansen hpj at urpla.net
Tue Nov 23 13:46:15 GMT 2010


[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




More information about the PyQt mailing list