我正在使用wxWidgets为我的C ++编程类开发一个程序。我遇到了一个很大的问题,我的事件处理程序(我假设)没有被调用,因为当我点击按钮触发事件时,没有任何反应。我的问题是:你能帮助我找到问题并解释为什么他们不会被调用吗?
事件处理程序OnAbout和OnQuit正在运行,而不是OnCompute或OnClear。我真的很沮丧,因为我无法弄清楚这一点。提前一堆谢谢!
#include "wx/wx.h"
#include "time.h"
#include <string>
using std::string;
// create object of Time class
Time first;
class App: public wxApp
{
virtual bool OnInit();
};
class MainPanel : public wxPanel
{
public:
// Constructor for panel class
// Constructs my panel class
// Params - wxWindow pointer
// no return type
// pre-conditions: none
// post-conditions: none
MainPanel(wxWindow* parent);
// OnCompute is the event handler for the Compute button
// params - none
// preconditions - none
// postconditions - tasks will have been carried otu successfully
// returns void
void OnCompute(wxCommandEvent& WXUNUSED(event));
// OnClear is the event handler for the Clear button
// params - none
// preconditions - none
// postconditions - all text areas will be cleared of data
// returns void
void OnClear(wxCommandEvent& WXUNUSED(event));
// Destructor for panel class
// params none
// preconditions - none
// postconditions - none
// no return type
~MainPanel( );
private:
wxStaticText *startLabel;
wxStaticText *endLabel;
wxStaticText *pCLabel;
wxStaticText *newEndLabel;
wxTextCtrl *start;
wxTextCtrl *end;
wxTextCtrl *pC;
wxTextCtrl *newEnd;
wxButton *compute;
wxButton *clear;
DECLARE_EVENT_TABLE()
};
class MainFrame: public wxFrame
{
private:
wxPanel *mainPanel;
public:
MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
~MainFrame();
DECLARE_EVENT_TABLE()
};
enum
{
ID_Quit = 1,
ID_About,
BUTTON_COMPUTE = 100,
BUTTON_CLEAR = 200
};
IMPLEMENT_APP(App)
BEGIN_EVENT_TABLE(MainFrame, wxFrame)
EVT_MENU(ID_Quit, MainFrame::OnQuit)
EVT_MENU(ID_About, MainFrame::OnAbout)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(MainPanel, wxPanel)
EVT_MENU(BUTTON_COMPUTE, MainPanel::OnCompute)
EVT_MENU(BUTTON_CLEAR, MainPanel::OnClear)
END_EVENT_TABLE()
bool App::OnInit()
{
MainFrame *frame = new MainFrame( _("Good Guys Delivery Time Calculator"), wxPoint(50, 50),
wxSize(450,340) );
frame->Show(true);
SetTopWindow(frame);
return true;
}
MainPanel::MainPanel(wxWindow* parent) : wxPanel(parent)
{
startLabel = new wxStaticText(this, -1, "Start Time:", wxPoint(75, 35));
start = new wxTextCtrl(this, -1, "", wxPoint(135, 35), wxSize(40, 21));
endLabel = new wxStaticText(this, -1, "End Time:", wxPoint(200, 35));
end = new wxTextCtrl(this, -1, "", wxPoint(260, 35), wxSize(40, 21));
pCLabel = new wxStaticText(this, -1, "Percent Change:", wxPoint(170, 85));
pC = new wxTextCtrl(this, -1, "", wxPoint(260, 85), wxSize(40, 21));
newEndLabel = new wxStaticText(this, -1, "New End Time:", wxPoint(180, 130));
newEnd = new wxTextCtrl(this, -1, "", wxPoint(260, 130), wxSize(40, 21));
compute = new wxButton(this, BUTTON_COMPUTE, "Compute", wxPoint(135, 185), wxSize(75, 35));
clear = new wxButton(this, BUTTON_CLEAR, "Clear", wxPoint(230, 185), wxSize(75, 35));
}
MainPanel::~MainPanel() {}
MainFrame::MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame( NULL, -1, title, pos, size )
{
mainPanel = new MainPanel(this);
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, _("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _("E&xit") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, _("&File") );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( _("Hi") );
}
MainFrame::~MainFrame() {}
void MainFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(TRUE);
}
void MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox( _("Alex Olson\nProject 11"),
_("About"),
wxOK | wxICON_INFORMATION, this);
}
void MainPanel::OnCompute(wxCommandEvent& WXUNUSED(event))
{
int startT;
int endT;
int newEndT;
double tD;
wxString startTString = start->GetValue();
wxString endTString = end->GetValue();
startT = wxAtoi(startTString);
endT = wxAtoi(endTString);
pC->GetValue().ToDouble(&tD);
first.SetStartTime(startT);
first.SetEndTime(endT);
first.SetTimeDiff(tD);
try {
first.ValidateData();
newEndT = first.ComputeEndTime();
*newEnd << newEndT;
}
catch (BaseException& e) {
wxMessageBox(_(e.GetMessage()),
_("Something Went Wrong!"),
wxOK | wxICON_INFORMATION, this);
}
}
void MainPanel::OnClear(wxCommandEvent& WXUNUSED(event))
{
start->Clear();
end->Clear();
pC->Clear();
newEnd->Clear();
}
答案 0 :(得分:3)
EVT_MENU(BUTTON_COMPUTE, mainPanel中:: OnCompute) EVT_MENU(BUTTON_CLEAR, mainPanel中:: OnClear)
在上述陈述中,使用EVT_BUTTON而不是EVT_MENU。
答案 1 :(得分:1)
我想我明白了。在MainPanel
的事件表中,而不是EVT_MENU
使用EVT_BUTTON
。