2010-03-25 6 views
5

Estoy trabajando en un programa para mi clase de programación C++, usando wxWidgets. Tengo un gran problema porque mis manejadores de eventos (supongo) no están siendo llamados, porque cuando hago clic en el botón para desencadenar el evento, no pasa nada. Mi pregunta es: ¿me pueden ayudar a encontrar el problema y explicar por qué no los llamarán?Controladores de eventos que no reciben la llamada? - wxWidgets

Los controladores de eventos OnAbout y OnQuit funcionan, pero no OnCompute u OnClear. Estoy muy frustrado ya que no puedo resolver esto. ¡Muchas gracias de antemano!

#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(); 
} 

Respuesta

3

EVT_MENU (BUTTON_COMPUTE, MainPanel :: OnCompute) EVT_MENU (BUTTON_CLEAR, MainPanel :: su dudoso)

En las declaraciones anteriores EVT_BUTTON utilizar en lugar de EVT_MENU.

+0

¡Gracias TANTO! – Alex

1

Creo que lo veo. En la tabla de eventos para MainPanel en lugar de EVT_MENU, use EVT_BUTTON.

Cuestiones relacionadas