[PyQt] Resize columns in QTreeViewv

Hans Meine meine at informatik.uni-hamburg.de
Wed Dec 1 14:00:26 GMT 2010

Hi again!

Op den Dunnersdag 25 November 2010 Klock 21:15:19 hett Gionata Boccalini 
> Ok, don't worry, I absolutely don't want to waste your time!! But what
> do you mean with
> > In
> > general, your code could (and should) be much more concise.

OK, I had a look again, your code is not much too complex actually.

Anyhow, there's potential for optimization IMHO:

- [duplicate/complex code] remove all QTimer and resizeColumnToContents stuff, 
instead use self.__fileView.header().setResizeMode(0, 
QtGui.QHeaderView.ResizeToContents) *

- for the minimal example for this list, the updateSelection() stuff is 
unnecessary (the smaller the example, the more likely you'll get answers)

- [consistency] rename __treeView to __dirView

- [simplicity] instead of connecting to clicked/expanded on the left, what 
about currentChanged?

- [duplicate code] in any case, don't setup the filemodel's root in __init__, 
but make sure that the connected signals are emitted - in my case, I would 
call setCurrentIndex - in order to let updateFiles() to the job

- [duplicate code] setup the treeviews with your visual preferences only once

- maybe disable expansion in the __fileView (and decoration)

Attached you find a refactored version of your example.

Then, it is easy to solve your problem by just re-creating the model in every 
updateFiles() step.  I am attaching that, too.

* I just realized that manual resizing might still be desirable.  Anyhow, the 
timer solution looks quite hacky to my eyes.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: file_browser_refactored.py
Type: text/x-python
Size: 2386 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20101201/c66c11a8/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: file_browser_solved.py
Type: text/x-python
Size: 2375 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20101201/c66c11a8/attachment-0001.py>

More information about the PyQt mailing list