wxQt: Make wxListCtrl::SetColumnWidth() conformant to documentation

Also update the listctrl sample to show it's effect on the second column
wxLIST_AUTOSIZE (Ctrl+R) to resize the column to content
wxLIST_AUTOSIZE_USEHEADER (Ctrl+Shift+R) to resize it to header section
This commit is contained in:
ali kettab 2022-12-09 23:13:36 +01:00
parent 4192b28a7f
commit 51d813cab7
4 changed files with 58 additions and 5 deletions

View file

@ -1077,7 +1077,7 @@ public:
@c wxLIST_AUTOSIZE will resize the column to the length of its longest item.
@c wxLIST_AUTOSIZE_USEHEADER will resize the column to the length of the
header (Win32) or 80 pixels (other platforms).
header (wxMSW and wxQt) or 80 pixels (other platforms).
In small or normal icon view, @a col must be -1, and the column width is set
for all columns.

View file

@ -151,6 +151,8 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(LIST_TOGGLE_HEADER, MyFrame::OnToggleHeader)
EVT_MENU(LIST_TOGGLE_BELL, MyFrame::OnToggleBell)
EVT_MENU(LIST_CHECKVISIBILITY, MyFrame::OnCheckVisibility)
EVT_MENU(LIST_AUTOSIZE, MyFrame::OnAutoResize)
EVT_MENU(LIST_AUTOSIZE_USEHEADER, MyFrame::OnAutoResize)
EVT_MENU(LIST_FIND, MyFrame::OnFind)
EVT_MENU(LIST_TOGGLE_CHECKBOX, MyFrame::OnToggleItemCheckBox)
EVT_MENU(LIST_GET_CHECKBOX, MyFrame::OnGetItemCheckBox)
@ -260,6 +262,9 @@ MyFrame::MyFrame(const wxString& title)
menuList->AppendCheckItem(LIST_TOGGLE_BELL, "Toggle &bell on no match");
menuList->Append( LIST_CHECKVISIBILITY, "Check if lines 2 and 9 are visible" );
menuList->AppendSeparator();
menuList->Append( LIST_AUTOSIZE, "Auto resize column 2\tCtrl-R" );
menuList->Append( LIST_AUTOSIZE_USEHEADER, "Auto resize column 2 (use header)\tCtrl-Shift-R" );
menuList->AppendSeparator();
menuList->AppendCheckItem(LIST_TOGGLE_CHECKBOXES,
"&Enable Checkboxes");
menuList->Check(LIST_TOGGLE_CHECKBOXES, true);
@ -379,6 +384,20 @@ void MyFrame::OnCheckVisibility(wxCommandEvent& WXUNUSED(event))
wxLogMessage( "Line 9 is not visible" );
}
void MyFrame::OnAutoResize(wxCommandEvent& event)
{
if ( event.GetId() == LIST_AUTOSIZE )
{
wxLogMessage( "Column 2 resized to content" );
m_listCtrl->SetColumnWidth(1, wxLIST_AUTOSIZE);
}
else
{
wxLogMessage( "Column 2 resized to header" );
m_listCtrl->SetColumnWidth(1, wxLIST_AUTOSIZE_USEHEADER);
}
}
void MyFrame::OnGoTo(wxCommandEvent& WXUNUSED(event))
{
if ( m_listCtrl->IsEmpty() )

View file

@ -117,6 +117,7 @@ protected:
void OnVirtualView(wxCommandEvent& event);
void OnSmallVirtualView(wxCommandEvent& event);
void OnCheckVisibility(wxCommandEvent& event);
void OnAutoResize(wxCommandEvent& event);
void OnSetItemsCount(wxCommandEvent& event);
@ -243,5 +244,7 @@ enum
LIST_THAW,
LIST_TOGGLE_LINES,
LIST_CHECKVISIBILITY,
LIST_AUTOSIZE,
LIST_AUTOSIZE_USEHEADER,
LIST_CTRL = 1000
};

View file

@ -669,6 +669,8 @@ public:
endInsertColumns();
m_listCtrl->SetColumnWidth(newColumnIndex, info.m_width);
return true;
}
@ -1156,13 +1158,42 @@ int wxListCtrl::GetColumnWidth(int col) const
bool wxListCtrl::SetColumnWidth(int col, int width)
{
if ( width < 0 )
const auto header = m_qtTreeWidget->header();
if ( header &&
col == GetColumnIndexFromOrder(col) &&
col == GetColumnCount() - 1 )
{
m_qtTreeWidget->resizeColumnToContents(width);
return true;
// Always stretch the last section if _with_ is either
// wxLIST_AUTOSIZE or wxLIST_AUTOSIZE_USEHEADER
header->setStretchLastSection( width < 0 );
}
m_qtTreeWidget->setColumnWidth(col, width);
if ( width >= 0 )
m_qtTreeWidget->setColumnWidth(col, width);
else
{
if ( width == wxLIST_AUTOSIZE_USEHEADER )
{
const auto header = m_qtTreeWidget->header();
const QHeaderView::ResizeMode oldResizeMode = header->sectionResizeMode(col);
header->setSectionResizeMode(col, QHeaderView::ResizeToContents);
header->resizeSection(col, header->defaultSectionSize()); // passing any value > 0 is ok
header->setSectionResizeMode(col, oldResizeMode);
}
else // wxLIST_AUTOSIZE
{
// Temporarily hide the header if it's shown as we don't want the header section
// to be considered by resizeColumnToContents() because it's size will be honored
// if it's larger than the column content.
const bool wasHidden = m_qtTreeWidget->isHeaderHidden();
m_qtTreeWidget->setHeaderHidden(true);
m_qtTreeWidget->resizeColumnToContents(col);
m_qtTreeWidget->setHeaderHidden(wasHidden);
}
}
return true;
}