【问题标题】:vxFloatingPointValidator not passing values to the associated variablevxFloatingPointValidator 未将值传递给关联变量
【发布时间】:2013-01-26 18:34:57
【问题描述】:

在我的项目(Visual C++ 2010 Express,wxWidgets 2.9.4)中,我使用了一些 TextCtrl 对象,这些对象应该通过 wxFloatingPointValidator 与三个变量(a0、a1、a2 - 表示抛物线的系数)相关联.然后将这些用于使用 wxMathPlot 绘制函数。系数是定义其他所有内容的 wxFrame 类的成员。该程序围绕“计算”按钮旋转 - 触发 OnCalculate() 方法,该方法计算函数的 x 和 y 坐标并向 m_plot wxMathPlot 对象添加一个图层。 出于某种原因,验证器根本没有将输入值传递给成员字段(我在调试模式下跟踪了这一点)。我一直在参考一些示例代码 sn-ps,例如:http://docs.wxwidgets.org/trunk/classwx_floating_point_validator.html 在我看来,我的实现几乎相同,但没有达到预期的效果。任何帮助将不胜感激...

注意:我尝试了不同的方法,例如将验证器和文本框放在向量中以使代码更整洁,但使用这个版本是为了让我在做什么更明显。此外,过去没有 a0、a1 和 a2 的默认值具有相同的效果(它在 y=0 处绘制一条直线)。

编辑:我还尝试将变量 a0、a1 和 a2 放在一个单独的类中,并像在官方 wxValidator 示例中那样全局定义它们 -> 效果相同......

#include "wx/wx.h"
#include <wx/valnum.h>      // wxFloatingPointValidator
#include <wx/bookctrl.h>    // make tabs in the window
#include "mathplot.h"       // plotting
#include <vector>

class MyApp: public wxApp
{
    virtual bool OnInit();
};

class MyFrame: public wxFrame
{
public:
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
    wxBookCtrl * book;
    wxTextCtrl * _textBox0, * _textBox1, * _textBox2;
    double a0,a1,a2;
    std::vector<double> _xcoords, _ycoords;
    mpWindow * m_plot;

    void OnQuit(wxCommandEvent& event);
    void OnCalculate(wxCommandEvent& event);

    DECLARE_EVENT_TABLE()
};

enum
{
    ID_Quit = 1,
    ID_textBox,
    BUTTON_calculate
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(ID_Quit, MyFrame::OnQuit)
    EVT_BUTTON(BUTTON_calculate, MyFrame::OnCalculate)
END_EVENT_TABLE()

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame( _("wxPlotter"), wxPoint(50, 50), wxSize(450,340) );
    frame->Show(true);
    SetTopWindow(frame);
    return true;
}

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame( NULL, -1, title, pos, size )
{
    a0=0; a1=0; a2=0;   // default values

    wxMenu *menuFile = new wxMenu;
    menuFile->Append( ID_Quit, _("&Exit") );
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append( menuFile, _("&File") );
    SetMenuBar( menuBar );

    book = new wxBookCtrl(this, -1);

    wxPanel *panel1 = new wxPanel(book);

    wxFloatingPointValidator<double> _val0(4,&a0,wxNUM_VAL_DEFAULT);
    _textBox0 = new wxTextCtrl(panel1, ID_textBox, "0.0000", wxPoint(40, 30), wxSize(60,20), wxTE_PROCESS_ENTER, _val0, wxTextCtrlNameStr);

    wxFloatingPointValidator<double> _val1(4,&a1,wxNUM_VAL_DEFAULT);
    _textBox1 = new wxTextCtrl(panel1, ID_textBox, "0.0000", wxPoint(40, 60), wxSize(60,20), wxTE_PROCESS_ENTER, _val1, wxTextCtrlNameStr);

    wxFloatingPointValidator<double> _val2(4,&a2,wxNUM_VAL_DEFAULT);
    _textBox2 = new wxTextCtrl(panel1, ID_textBox, "0.0000", wxPoint(40, 90), wxSize(60,20), wxTE_PROCESS_ENTER, _val2, wxTextCtrlNameStr);

    new wxButton( panel1, BUTTON_calculate, _T("Calculate"), wxPoint(150,30), wxSize(100,30) );
    book->AddPage(panel1, _T("Input"), true);   // move the page to the tab

    wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
    wxPanel *panel2 = new wxPanel(book);
    panel2->SetSizer(topsizer);

    m_plot = new mpWindow( panel2, -1, wxPoint(0,0), wxSize(100,100), wxSUNKEN_BORDER );
    m_plot->SetMargins(0,0,50,70);
    mpScaleX* xaxis = new mpScaleX(wxT("x"), mpALIGN_BOTTOM, true);
    mpScaleY* yaxis = new mpScaleY(wxT("y"), mpALIGN_LEFT, true);
    xaxis->SetDrawOutsideMargins(false);
    yaxis->SetDrawOutsideMargins(false);
    m_plot->AddLayer(xaxis);
    m_plot->AddLayer(yaxis);
    m_plot->Fit(-5.,5.,-5,5.,0,0);
    topsizer->Add( m_plot, 1, wxEXPAND );
    book->AddPage(panel2, _T("Plot"), false);   // false to make this a secondary tab -> not displayed upon initialisation
}

void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))   // defines what happens when quit is pressed
{
    Close(TRUE);    // closes the window
}

void MyFrame::OnCalculate(wxCommandEvent& WXUNUSED(event))
{
    for (int i=0; i<1000; i++)
    {
        _xcoords.push_back(i*.1);
        _ycoords.push_back(a0+a1*_xcoords[i]+a2*pow(_xcoords[i],2.));
    }

    mpFXYVector* vectorLayer = new mpFXYVector(_("Parabola"));

    vectorLayer->SetData(_xcoords, _ycoords);
    vectorLayer->SetContinuity(true);
    wxPen vectorpen(*wxBLUE, 2, wxSOLID);
    vectorLayer->SetPen(vectorpen);
    vectorLayer->SetDrawOutsideMargins(false);

    m_plot->AddLayer(vectorLayer);
    m_plot->Fit(-5.,5.,-5,5.,0,0);
}

【问题讨论】:

    标签: c++ user-interface wxwidgets


    【解决方案1】:

    验证器仅在wxDialog 中自动使用。由于您在这里没有使用,因此您必须在适当的时候自己致电TransferData{To,From}Window()。请注意,您确实不需要像您所做的那样在验证器本身上调用它,在父窗口上调用它会对其中的所有验证器执行此操作。

    【讨论】:

    • 这解释了为什么在示例代码中他们使用 wxDialog 并且无需调用此函数即可工作。感谢你的回答。您的意见确实推动了我的第一个 wxWidgets 项目向前发展,非常感谢!
    【解决方案2】:

    我设法通过在 OnCalculate() 方法中使用以下内容来解决问题:

    _textBox0->GetValidator()->TransferFromWindow();
    

    这利用了将数据从 wxTextCtrl 对象传输到关联变量的内置函数。另一种解决方法是直接在 TextCtrl 上调用 GetValue() 方法,但这不会通过验证器 - 猜测这取决于应用程序和您想要实现的目标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 2020-10-30
      • 2013-05-04
      • 2014-05-14
      • 1970-01-01
      相关资源
      最近更新 更多