gpt4 book ai didi

c++ - 事件处理程序未被调用? - wxWidgets

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:08 25 4
gpt4 key购买 nike

我正在为我的 C++ 编程类(class)开发一个程序,使用的是 wxWidgets。我有一个很大的问题,我的事件处理程序(我假设)没有被调用,因为当我点击按钮触发事件时,没有任何反应。我的问题是:你能帮我找到问题并解释为什么他们没有接到电话吗?

事件处理程序 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();
}

最佳答案

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

在上面的语句中使用 EVT_BUTTON 而不是 EVT_MENU。

关于c++ - 事件处理程序未被调用? - wxWidgets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2513048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com