2010-03-25 40 views
5

Tôi đang làm việc trên một chương trình cho lớp lập trình C++ của mình, sử dụng wxWidgets. Tôi đang gặp một vấn đề lớn trong việc xử lý sự kiện của tôi (tôi giả định) không được gọi, bởi vì khi tôi nhấp vào nút để kích hoạt sự kiện, không có gì xảy ra. Câu hỏi của tôi là: Bạn có thể giúp tôi tìm ra vấn đề và giải thích tại sao họ không được gọi?Trình xử lý sự kiện không được gọi? - wxWidgets

Trình xử lý sự kiện OnAbout và OnQuit đang hoạt động, không chỉ là OnCompute hoặc OnClear. Tôi thực sự thất vọng vì tôi không thể hình dung ra điều này. Cảm ơn trước rất nhiều!

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

Trả lời

3

EVT_MENU (BUTTON_COMPUTE, MainPanel :: OnCompute) EVT_MENU (BUTTON_CLEAR, MainPanel :: OnClear)

Trong báo cáo trên sử dụng EVT_BUTTON thay vì EVT_MENU.

+0

Cảm ơn bạn SO MUCH !!! – Alex

1

Tôi nghĩ là tôi đã thấy. Trong bảng sự kiện cho MainPanel thay vì EVT_MENU sử dụng EVT_BUTTON.

Các vấn đề liên quan