wxWidgets: wxListCtrl column width

I'm learning the wxWidgets GUI library, and I'm having trouble getting the wxListCtrl column width adjusted correctly.

-1 is automatically sized, and according to the documentation, it's supposed to size the column = to the largest element in the list control. That's not happening though. The list control has the following styles: wxLC_VRULES, wxLC_SINGLE_SEL, wxLC_NO_HEADER, wxLC_VIRTUAL, wxLC_REPORT. Minimum size: 200, 175. I dynamically update the size of the list according to the number of elements in a vector so that when the control requests an element it's within a valid range (i.e.: it doesn't crash the program).

Am I doing somthing wrong here? Is there a correct way to size the column? Am I missing somthing?

If any other information is needed, don't hesitate to ask for it.

Thanks for the help.
The adjustment doesn't happen each time you insert an item. It happens when you explicitely call SetColumnWidth(...). Plus there is a difference between wxLIST_AUTOSIZE and wxLIST_AUTOSIZE_USEHEADER.

I made this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  void ListCtrl::AutoSizeHeader(wxListCtrl *const list_ctrl)
  {
    if(list_ctrl)
    {
      for(int i = 0; i < list_ctrl->GetColumnCount(); ++i)
      {
        list_ctrl->SetColumnWidth(i, wxLIST_AUTOSIZE);
        const int a_width = list_ctrl->GetColumnWidth(i);
        list_ctrl->SetColumnWidth(i, wxLIST_AUTOSIZE_USEHEADER);
        const int h_width = list_ctrl->GetColumnWidth(i);
        list_ctrl->SetColumnWidth(i, (std::max)(a_width, h_width));
      }
    }
  }
in order to adjust the column size. It is called after the wxListCtrl is completely filled.
This is a virtual list, so the list can't be completely filled... Items are added on request.
Last edited on
You can do that after each request / for each column. The reason why it is not done automatically is that it would flicker if you do that for like 1000 elements in a loop.
Topic archived. No new replies allowed.