我在应用程序中使用wxWidgets 2.8 和wxPropertyGrid 1.4 。对于浮点值,我想使用滑块来编辑它们。但是,默认情况下不提供滑块编辑器,因此我采用了自己的编辑器,遵循文档中提到的指南。
然而,使用这个新的编辑器,即使我将它设置为我的浮动属性的编辑器,它实际上并没有出现,直到属性网格单元以任何方式交互(例如单击)。在那之前,经典的基于文本框的控制器仍然可见。
显然,滑块编辑器的实际 CreateControl 方法在生成 propgrid 时不会被调用 - 它仅在单元格本身以某种方式交互时被调用。
这是我的自定义属性编辑器:
wxpgslider.h
类 WXDLLIMPEXP_PG wxPGSliderEditor : 公共 wxPGEditor
{
#ifndef 痛饮
WX_PG_DECLARE_EDITOR_CLASS(wxPGSliderEditor)
#万一
上市:
wxPGSliderEditor (int p = 10000)
: 精度(p)
{
}
~wxPGSliderEditor ()
{}
// CreateControls 方法存根的宏
wxPG_DECLARE_CREATECONTROLS
void UpdateControl ( wxPGProperty* 属性, wxWindow* wnd) const;
bool OnEvent ( wxPropertyGrid* propgrid, wxPGProperty* property, wxWindow* wnd, wxEvent& event) const;
bool GetValueFromControl ( wxVariant& 变体, wxPGProperty* 属性, wxWindow* wnd) const;
void SetValueToUnspecified ( wxPGProperty* 属性, wxWindow* wnd) const;
//void DrawValue ( wxDC& dc, const wxRect& rect, wxPGProperty* property, const wxString& text) const;
私人的:
整数精度;
};
wxpgslider.cpp
#include "cseditor/wxpgslider.h"
//----------------- wxPGSliderEditor ---------
WX_PG_IMPLEMENT_EDITOR_CLASS(SliderEditor, wxPGSliderEditor, wxPGEditor)
wxPGWindowList wxPGSliderEditor::CreateControls( wxPropertyGrid* propgrid,
wxPGProperty* 属性,
const wxPoint& pos,
常量 wxSize& 大小) 常量
{
double v_d = property->GetValue().GetDouble();
如果 ( v_d 1 )
v_d = 1;
wxSlider *ctrl = new wxSlider();
#ifdef __WXMSW__
ctrl->隐藏();
#万一
ctrl->创建 (propgrid->GetPanel(),
wxPG_SUBID2,
(int)(v_d * 精度),
0,
精确,
位置,
尺寸,
wxSL_HORIZONTAL );
返回 wxPGWindowList(ctrl);
}
void wxPGSliderEditor::UpdateControl ( wxPGProperty* 属性, wxWindow* wnd ) const
{
wxSlider* ctrl = wxDynamicCast (wnd, wxSlider);
如果 ( ctrl )
{
双倍价值;
if (wxPGVariantToDouble (property->DoGetValue(), &val))
{
如果 ( 值 1 )
值 = 1;
ctrl->SetValue ( (int)(val * precision) );
//static_cast(属性)->GetLabel()
// ->SetValue( wxString::Format(wxT("%ld"), val * precision) );
}
}
}
bool wxPGSliderEditor::OnEvent ( wxPropertyGrid* propgrid,
wxPGProperty* 属性,
wxWindow* wnd,
wxEvent& 事件 ) 常量
{
if(event.GetEventType() == wxEVT_SCROLL_CHANGED)
{
// 更新值
事件.Skip();
propgrid->EditorsValueWasModified();
返回真;
}
返回假;
}
bool wxPGSliderEditor::GetValueFromControl ( wxVariant& 变体,
wxPGProperty* 属性,
wxWindow* wnd ) 常量
{
wxSlider* ctrl = wxDynamicCast (wnd, wxSlider);
如果 ( ctrl )
{
变体 = wxVariant ( (double)(ctrl->GetValue())/(double)(precision) );
属性->SetValue(变体);
}
返回真;
}
void wxPGSliderEditor::SetValueToUnspecified ( wxPGProperty* 属性, wxWindow* wnd) const
{
wxSlider* ctrl = wxDynamicCast (wnd, wxSlider);
如果 ( ctrl )
{
ctrl->设置值(0);
}
}
这是我用来在 Populate 函数中生成滑块的代码:
双值 = 变体->GetFloat();
// 生成一个自制的滑块
wxFloatProperty* fp = new wxFloatProperty(translatedName, originalName, value);
wxPGEditor* rHandle = wxPropertyGrid::RegisterEditorClass(new wxPGSliderEditor(), wxT("SliderEditor"));
fp->SetEditor(rHandle);
page->AppendIn(categoryID, fp);
我注册了这个类,以防它之前没有注册过,然后设置属性的编辑器。然后我将属性添加到网格中。为什么在与单元格交互之前滑块不显示?
调用pgMan->GetGrid()->SelectProperty(fp, false);是使它被绘制的唯一方法吗?