Get wxString from wxListCtrl by index and column number.(Need to modify the source code of wxWidgets)
想直接用的可以跳過代碼前的中文,直接參考改動便是
此改動的起因是我繼承wxListCtrl寫了一個新類,想要做出表頭點擊排序的效果,但是通過外部vector數據綁定帶來的問題是,數據過多的時候,點擊另一個列的表頭排序,需要重新填裝數據,才能確定每行原本的順序,然後再用wxListCtrl::SetItemData填裝排序數據,然後再wxListCtrl::SortItem,這樣明顯速度會比較慢。
合理的辦法是:需要切換到另一列排序時,直接通過給定行數和列數讀取wxListCtrl中的當前字串,按照需要的規則轉成數據,然後用wxListCtrl::SetItemData填裝,事實上,這樣的確速度快了很多,但是中間遇到的問題是,我們親愛的wxWidgets沒有賦予wxListCtrl這個功能! wxListCtrl::GetItemText這些我看了半天文檔,貌似都是直接返回第一列的值,所以只好自己動手豐衣足食了。。。
再囉嗦兩句解釋下改動,wxListCtrl是繼承自wxGenericListCtrl這個類,而wxListMainWindow是wxGenericListCtrl這個類中的一個成員,我們其實是通過這個成員wxListMainWindow::GetLine()獲取的某一行的wxListLineData,然後用wxListLineData::GetText(int col)獲取的莫列上的字串。
改動示例採用版本是2.8.10,其他版本如有變更,照樣子
New function:
wxListCtrl::GetTextByIndexCol(long index, int col)
For getting the text by given index and column number.
[xxx@dev01 wxWidgets-2.8.10]$ diff -c ../wx/wxWidgets-2.8.10/src/generic/listctrl.cpp src/generic/listctrl.cpp
*** ../wx/wxWidgets-2.8.10/src/generic/listctrl.cpp 2009-03-06 20:10:57.000000000 +0800
--- src/generic/listctrl.cpp 2011-08-05 21:47:54.267411851 +0800
***************
*** 505,510 ****
--- 505,512 ----
virtual ~wxListMainWindow();
+ wxString GetTextByIndexCol(long index, int col) const;
+
bool HasFlag(int flag) const { return m_parent->HasFlag(flag); }
// return true if this is a virtual list control
***************
*** 4668,4673 ****
--- 4670,4682 ----
return wxNOT_FOUND;
}
+
+ wxString wxListMainWindow::GetTextByIndexCol(long index, int col) const
+ {
+ wxListLineData *line = this->GetLine((size_t)index);
+ return line->GetText( col );
+ }
+
long wxListMainWindow::FindItem(long start, wxUIntPtr data)
{
long pos = start;
***************
*** 4995,5000 ****
--- 5004,5014 ----
delete m_imageListState;
}
+ wxString wxGenericListCtrl::GetTextByIndexCol(long index, int col) const
+ {
+ return m_mainWin->GetTextByIndexCol(index, col);
+ }
+
void wxGenericListCtrl::CalculateAndSetHeaderHeight()
{
if ( m_headerWin )
[xxx@dev01 wxWidgets-2.8.10]$ diff -c ../wx/wxWidgets-2.8.10/include/wx/generic/listctrl.h include/wx/generic/listctrl.h
*** ../wx/wxWidgets-2.8.10/include/wx/generic/listctrl.h 2009-03-06 20:10:58.000000000 +0800
--- include/wx/generic/listctrl.h 2011-08-05 21:48:19.279480048 +0800
***************
*** 52,57 ****
--- 52,59 ----
}
virtual ~wxGenericListCtrl();
+ wxString GetTextByIndexCol(long index, int col) const;
+
bool Create( wxWindow *parent,
wxWindowID winid = wxID_ANY,
const wxPoint &pos = wxDefaultPosition,
改動不多,就兩個文件,改完以後記得再make和make install一下。(前提是你原來就用源代碼自己編譯安裝的。)
然後就可以使用新添加的函數wxListCtrl::GetTextByIndexCol(long index, int col),參數就是行列,返回就是wxString