【问题标题】:wxPropertyGrid custom slider property issueswxPropertyGrid 自定义滑块属性问题
【发布时间】:2012-08-14 01:12:48
【问题描述】:

我在一个应用程序中使用 wxWidgets 2.8 和 wxPropertyGrid 1.4。 对于浮点值,我想使用滑块来编辑它们。但是,默认情况下不提供滑块编辑器,因此我采用了自己的编辑器,遵循文档中提到的指南。

但是,使用这个新编辑器,即使我将它设置为我的浮动属性的编辑器,它实际上并不会出现,直到属性网格单元以任何方式交互(例如单击)。在此之前,经典的基于文本框的控制器仍然可见。

显然,在生成 propgrid 时不会调用滑块编辑器的实际 CreateControl 方法 - 只有在与单元格本身以某种方式交互时才会调用它。

这是我的自定义属性编辑器:

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); 是让它被绘制的唯一方法吗?

【问题讨论】:

    标签: c++ user-interface wxwidgets


    【解决方案1】:

    你正在使用

    #ifdef __WXMSW__
      ctrl->Hide();
    #endif
    

    关于什么

    #ifdef __WXMSW__
        ctrl->Show();
    #endif
    

    样本:

    ....
      wxSlider* ctrl = new wxSlider();
    #ifdef __WXMSW__
        ctrl->Hide();
    #endif
    ....
    ctrl->Create ( propgrid->GetPanel(),
                 wxPG_SUBID2,
                 (int)(v_d * precision),
                 0,
                 precision,
                 pos,
                 size,
                 wxSL_HORIZONTAL );
    
    #ifdef __WXMSW__
        ctrl->Show();
    #endif
    
    return wxPGWindowList(ctrl);
    }
    

    编辑

    我在您的代码中看不到 OnCustomEditorEvent。

    使用 wxEVT_SCROLL_THUMBTRACK 或 wxEVT_SCROLL_CHANGED。

    ....
    
    propgrid->Connect( wxPG_SUBID2, wxEVT_SCROLL_CHANGED,
                           (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction)
                           &wxPropertyGrid::OnCustomEditorEvent, NULL, propgrid );
    
    #ifdef __WXMSW__
      ctrl->Show();
    #endif
    
    return wxPGWindowList(ctrl);
    }
    

    【讨论】:

    • 谢谢,但遗憾的是,这实际上不是问题所在。你看,整个函数(CreateControls)在单元格被聚焦之前不会被调用。恕我直言,它应该在初始化所有其他网格控件时被调用,并且滑块应该保持在那里,而不是我明确替换的默认文本控件。
    猜你喜欢
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多