【问题标题】:How do I handle the wxEVT_GRID_COL_SORT event to sort a grid?如何处理 wxEVT_GRID_COL_SORT 事件以对网格进行排序?
【发布时间】:2012-12-27 00:40:51
【问题描述】:

我正在尝试对 wxGrid 进行排序。现在,the documentation 告诉我它不支持排序,但它确实会生成一个事件。该文档告诉我该事件称为wxEVT_GRID_COL_SORT。够公平的!

现在,问题是我根本不知道如何让活动正常进行。我的框架有一个事件表,看起来像这样:

BEGIN_EVENT_TABLE(MainWindow, wxFrame)
EVT_BUTTON(XRCID("toevoegknop"), MainWindow::openAddWindow)
// A few other events that work
END_EVENT_TABLE()

那里列出的事件已经很好地工作了。在我的 MainWindow 类中,我声明了一个函数:

void sortColumn(wxGridEvent& event);

现在,我想添加说wxEVT_GRID_COL_SORT 事件。从我的角度来看,文档并不清楚我应该做什么,所以我只是尝试通过将以下行添加到事件表来添加事件。

wxEVT_GRID_COL_SORT(MainWindow::sortColumn)

引发语法错误,因此它是不正确的。我注意到其他事件刚刚从 EVT 开始,所以我尝试删除 wx,但我仍然不走运。

通过广泛搜索互联网,我找到了 pastebin post,它通过将以下行添加到框架的构造函数(在我的例子中为 MainWindow)来处理事件

Grid->Connect(wxEVT_GRID_COL_SORT,(wxObjectEventFunction)&Frame::OnGridColSort);

我是这样改编的(MainWindow 的几乎整个构造函数)

MainWindow::MainWindow(const wxString& title, const wxPoint& pos, const wxSize& size, Collection* library, MovieDB* database)
: wxFrame(), library_(library), database_(database) {
wxXmlResource::Get()->LoadFrame(this, NULL, _T("hoofdvenster"));

SetSize(size);
grid_ = (wxGrid *)FindWindowById(XRCID("filmtabel"));
// Irrelevant code removed, setting up the grid labels etc.

grid_->Connect(wxEVT_GRID_COL_SORT,(wxObjectEventFunction)&MainWindow::sortColumn);
}

这会引发错误:

‘wxEVT_GRID_COL_SORT’未在此范围内声明

所以现在我不知道我还能尝试什么。请记住,我几天前才开始使用 wxWidgets,所以对于任何 wxWidgets 用户来说都是微不足道的事情可能不适合我。

提前致谢!

【问题讨论】:

    标签: c++ wxwidgets


    【解决方案1】:

    您使用的wx 2.8.12 似乎没有实现wxEVT_GRID_COL_SORT。它是在 wx 2.9 中添加的,因此您必须获得最新的开发版本 (2.9.4) 才能使用它。

    但是,在 wx 2.8 中,您可以处理 wxEVT_GRID_LABEL_LEFT_CLICK 并相应地调度事件以模拟事件。

    要么将事件添加到事件映射中,

    EVT_GRID_CMD_LABEL_LEFT_CLICK(ID_GRID,Frame::OnGridLabelLeftClick)
    

    或者在你的构造函数中连接它:

    grid->Connect(wxEVT_GRID_LABEL_LEFT_CLICK,
        (wxObjectEventFunction)&Frame::OnGridLabelLeftClick);
    
    void Frame::OnGridColSort(wxGridEvent& event) {}
    void Frame::OnGridRowSort(wxGridEvent& event) {}
    
    void Frame::OnGridLabelLeftClick(wxGridEvent& event) {
        // GetCol and GetRow will return the index of the col/row label clicked
        event.Skip(); // the next handler will select col/row/everything, based
                      // on the label clicked; remove to prevent selection
        if( event.GetCol() >= 0 )
            OnGridColSort(event);
        else if( event.GetRow() >= 0 )
            OnGridRowSort(event);
        else
            ; // if both are -1, the upper left corner was clicked (select all)
    }
    

    这将类似于EVT_GRID_COL_SORT

    【讨论】:

      猜你喜欢
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多