[wxWidgets]为wxListCtrl增加函数,给定行数列数获取字符串

来源:互联网 发布:python入门教程视频 编辑:程序博客网 时间:2024/05/17 20:30

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.


wxWidgets-2.8.10/src/generic/listctrl.cpp
[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 )


wxWidgets-2.8.10/include/wx/generic/listctrl.h
[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

原创粉丝点击