我正在使用 wxWidgets 3.1.0 并且我正在用 C++ 开发一个 Windows 应用程序。
我正在使用基础 wxGrid 并且我通过用鼠标 ( EnableDragColMove(true) ) 拖动它们启用了列重新排序。我现在的问题是,在将列拖到新位置后,我需要获取移动列的新位置/索引。
不幸的是,我无法从可用的 API 找到一种方法来做到这一点。
我 try catch wxGridEvent wxEVT_GRID_COL_MOVE 然后使用 GetCol() 和 GetColPos() 检查列的新索引:
gridDataList->Bind(wxEVT_GRID_COL_MOVE, &FormData::OnList_ColumnMove, this);
...
void FormData::OnList_ColumnMove(wxGridEvent& event)
{
int movedCol = event.GetCol();
int movedColPos = gridDataList->GetColPos(movedCol );
...
}
但似乎在 列实际移动之前触发了 事件,因此 GetColPos() 仍将返回当前列索引, 而不是 新索引。
列移动后似乎没有要捕获的事件。
我目前的解决方案/解决方法是:
不过,我想知道是否有一种更简洁、更简单的方法,而无需求助于上述解决方法。
任何建议表示赞赏。
最佳答案
是的,这个 wxEVT_GRID_COL_MOVE
是在移动列之前生成的,因为它可以被否决,从而防止移动发生。确实,如果它带有新的列位置会很方便,但不幸的是目前它没有(解决这个问题很简单,任何 patches doing this 都会受到欢迎!)。
using CallAfter()
稍后执行代码的标准解决方法应该可以正常工作,而无需更改 wxWidgets。即,假设您使用 C++11,您应该能够编写
void FormData::OnList_ColumnMove(wxGridEvent& event)
{
const int movedCol = event.GetCol();
CallAfter([movedCol]() {
int movedColPos = gridDataList->GetColPos(movedCol);
...
});
}
关于c++ - 如何在 wxGrid 中获取移动列的新索引?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42196986/