在我的项目(Visual C++ 2010 Express,wxWidgets 2.9.4)中,我使用了一些 TextCtrl 对象,这些对象应该通过 wxFloatingPointValidator 与三个变量(a0、a1、a2 - 表示抛物线的系数)相关联。然后使用 wxMathPlot 绘制函数。系数是定义其他所有内容的 wxFrame 类的成员。该程序围绕“计算”按钮旋转 - 触发 OnCalculate() 方法,该方法计算函数的 x 和 y 坐标并向 m_plot wxMathPlot 对象添加一个图层。出于某种原因,验证器根本没有将输入值传递给成员字段(我在调试模式下跟踪了这一点)。我一直在参考一些示例代码片段,例如: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);
}