How can I set a column of listctrl/ultimatelistctrl to its 'best width' ?

Hi there.

I have a listctrl in report view.
Now I want to set its columns to their ‘best width’ ( which means the width of the longest item or the header’s width if it’s greater than the longest item’s ).

From the documentation, however, it seems that either the longest item’s width or the header’s width can be set to.
Is there a workaround? Can I get the header’s width myself with some API ?

Moreover, I want to do it when user double-cliked the separator of column header, replacing the default behaviour ( which only adjusts the width to the longest item’s but not taking the header’s width into account ).
Is there any event can be caught when a separator of column headers double-clicked in listctrl/ultimatelistctrl ? I failed to find it in the documentation.

The event is also desired when using a listctrl with ListCtrlAutoWidthMixin.
Because I want to prevent users from double-clicking the last separator to adjust the last column, which will cause an unexpected behaviour of listctrl ( namely, the column shrinks but will not fill the extra space again ). You can see that in the official ‘listctrl’ demo.

Any replies would be appreciated.

I don’t think we have that kind of control over the native widget. You might give the UltimateListCtrl a try though. It’s pure Python so you can hack it a little easier.

···

Mike Driscoll

Blog: http://blog.pythonlibrary.org

Hi, Mike.
I can’t find any clues in ultimatelistctrl, either. Could you give me some hints?

lz

I haven’t actually used that widget for anything. I was hoping its author would pipe up and give us a clue.

···

Mike Driscoll

Blog: http://blog.pythonlibrary.org

Hi All,

I haven’t actually used that widget for anything. I was hoping its author would pipe up and give us a clue.

Here I am :smiley:

To the OP: there is currently no already-implemented way of resizing the columns of wx.ListCtrl/UltimateListCtrl based on their “best” size (i.e., the maximum between the longest item text in the list and the header text). However, it should be relatively trivial to implement (at least in ULC), by modifying the SetColumnWidth method. At the moment, this method has the following signature:

SetColumnWidth(col, width)

Sets the column width.

Parameters:

width – can be a width in pixels or wx.LIST_AUTOSIZE (-1) or wx.LIST_AUTOSIZE_USEHEADER (-2) or LIST_AUTOSIZE_FILL (-3). wx.LIST_AUTOSIZE will resize the column to the length of its longest item. wx.LIST_AUTOSIZE_USEHEADER will resize the column to the length of the header (Win32) or 80 pixels (other platforms). LIST_AUTOSIZE_FILL will resize the column fill the remaining width of the window.

We might add another option (something like LIST_AUTOSIZE_BEST = -4) which will automagically calculate the best width for a column. Note, however, that if you have many items in the list (or you are using a virtual list), this approach will not work very well. If you manage to create a patch to the SVN version of ULC, I’ll happily apply it. Otherwise I’ll try to do it myself when (if?) I get some time.

As for the event emitted when a separator of column headers is double-clicked, you may want to consider wx.EVT_LIST_COL_DRAGGING and Veto() the event.

Andrea.

“Imagination Is The Only Weapon In The War Against Reality.”
http://xoomer.alice.it/infinity77/

import PyQt4.QtGui

Traceback (most recent call last):

File “”, line 1, in

ImportError: No module named PyQt4.QtGui

import pygtk

Traceback (most recent call last):

File “”, line 1, in

ImportError: No module named pygtk

···

On 14 October 2011 15:50, Mike Driscoll wrote:

import wx

Another one to look at is ObjectListView - ObjectListView — ObjectListView v1.2 documentation

I use OLV quit a bit and like it a lot better then wx.ListCtrl. Just looked at the doc again and I don't see that it provides what you want out of the box - probably non of the wx.ListCtrl based controls will do this as it could be a very expensive and slow operation.

I think what you could do with any of these wx.ListCtrl based controls is set the "auto" with to use the header and then track the width of your data and reset the column with when you loaded all the data into the control.

Werner

···

On 10/14/2011 03:35 AM, lowZoom wrote:

Hi, Mike.
I can't find any clues in ultimatelistctrl, either. Could you give me some hints?