[PyQt] QTreeWidget and fixed column widths on window resize

Roberto Alsina ralsina at kde.org
Thu Jul 31 22:49:36 BST 2008


On Thursday 31 July 2008 18:24:10 Andreas Pakulat wrote:
> On 31.07.08 21:37:04, Chris M wrote:
> > Not sure if there is a 'cleaner' solution but overriding the event is
> > fairly simple, for example;
> >
> > class MyTreeWidget(QTreeWidget):
> >     def __init__(self, parent = None):
> >         super(MyTreeWidget, self).__init__(parent)
> >
> >     def resizeEvent(self, event):
> >         width = event.size().width()
> >         self.setColumnWidth(1, 100)
> >         self.setColumnWidth(2, width - 100)
>
> This doesn't take into account any headers or scrollbars so it won't
> work.
>
> What should work however is setting the last column to a fixed size via
> the horizontal headerview. See QTreeView.horizontalHeader() and
> QHeaderView.

Not really. From the docs: 

----------------

stretchLastSection : bool
This property holds whether the last visible section in the header takes up 
all the available space.
The default value is false.
Note: The horizontal headers provided by QTreeView are configured with this 
property set to true, ensuring that the view does not waste any of the space 
assigned to it for its header.
Also note: If the value is set to true, this property will override the resize 
mode set on the last section in the header.
   
----------------

You need to unset stretchLastSection first, then set it to a fixed size.
-- 
 ("\''/").__..-''"`-. .         Roberto Alsina
 `9_ 9  )   `-. (    ).`-._.`)  KDE Developer (MFCH)
 (_Y_.)' ._   ) `._`.  " -.-'  http://lateral.netmanagers.com.ar 
  _..`-'_..-_/ /-'_.'     The 6,855th most popular site of Slovenia   
(l)-'' ((i).' ((!.'       according to alexa.com (27/5/2007) 
                    
Debugging is twice as hard as writing the code in the first place. 
Therefore, if you write the code as cleverly as possible, you are, 
by definition, not smart enough to debug it. --Brian W. Kernighan   
-- 
 ("\''/").__..-''"`-. .         Roberto Alsina
 `9_ 9  )   `-. (    ).`-._.`)  KDE Developer (MFCH)
 (_Y_.)' ._   ) `._`.  " -.-'  http://lateral.netmanagers.com.ar 
  _..`-'_..-_/ /-'_.'     The 6,855th most popular site of Slovenia   
(l)-'' ((i).' ((!.'       according to alexa.com (27/5/2007) 
                    
Debugging is twice as hard as writing the code in the first place. 
Therefore, if you write the code as cleverly as possible, you are, 
by definition, not smart enough to debug it. --Brian W. Kernighan 


More information about the PyQt mailing list