gpt4 book ai didi

c++ - wxButton 不会触发事件。

转载 作者:行者123 更新时间:2023-11-28 06:39:28 28 4
gpt4 key购买 nike

这是我的代码。我现在的 ID 不同了,但问题是我按下按钮时似乎无法触发事件。我有点困惑为什么事件不会触发。它曾经在两个 ID 都是 wxID_ANY 时工作,但当我这样做时两个按钮做同样的事情,现在,当我声明一个新 ID 时,它们都不起作用。

标题:

#include <vector>
#include <wx/wx.h>
using namespace std;

const int MAX = 100;
class determinantsFrame : public wxFrame
{
public:
determinantsFrame();
virtual ~determinantsFrame();

void OnQuit(wxCommandEvent &event);
void OnAbout(wxCommandEvent &event);
void OnClose(wxCloseEvent &event);
void OnCalculateClick(wxCommandEvent &event);
void OnReset(wxCommandEvent &event);
double determinant(double matrix[MAX][MAX], int order);

private:

static const long ID_Calculate;
static const long ID_Reset;

wxPanel *MainPanel;
wxButton *Enter;
wxButton *Reset;
wxMenu *fileMenu;
wxMenu *helpMenu;
wxMenuBar *menuBar;
wxTextEntryDialog *td;
wxString str;
//vector<double> value;

DECLARE_EVENT_TABLE();
};

CPP:

#include <wx/wx.h>
#include "determinantsFrame.h"
#include <vector>
#include <stdio.h>
#include <cmath>
#include <vector>
using namespace std;

BEGIN_EVENT_TABLE(determinantsFrame, wxFrame)
EVT_MENU(wxID_ABOUT, determinantsFrame::OnAbout)
EVT_MENU(wxID_EXIT, determinantsFrame::OnQuit)
EVT_CLOSE(determinantsFrame::OnClose)
EVT_BUTTON(ID_Calculate, determinantsFrame::OnCalculateClick)
EVT_BUTTON(ID_Reset, determinantsFrame::OnReset)
END_EVENT_TABLE()

int n;
wxTextCtrl *numbers[100][100];

const long determinantsFrame::ID_Calculate = wxNewId();
const long determinantsFrame::ID_Reset = wxNewId();

void determinantsFrame::OnClose(wxCloseEvent &event)
{
exit(1);
}

void determinantsFrame::OnAbout(wxCommandEvent &event)
{
wxString msg;
msg.Printf(wxT("Hello and welcome to %s"), wxVERSION_STRING);
wxMessageBox(msg, wxT("About Determinants Calculator"));
}

void determinantsFrame::OnQuit(wxCommandEvent &event)
{
Close();
}

determinantsFrame::~determinantsFrame()
{
Destroy();
}

determinantsFrame::determinantsFrame() : wxFrame(NULL, wxID_ANY, wxT("Determinants Calculator"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE)
{
wxStaticBoxSizer *matrix;
wxFlexGridSizer *cells;
wxBoxSizer *subBox;
wxBoxSizer *mainBox;

fileMenu = new wxMenu;
helpMenu = new wxMenu;
menuBar = new wxMenuBar();

helpMenu->Append(wxID_ABOUT, wxT("&About"), wxT("Show about dialog"));
fileMenu->Append(wxID_EXIT, wxT("&Exit"), wxT("Quit this program"));

menuBar->Append(fileMenu, wxT("&File"));
menuBar->Append(helpMenu, wxT("&Help"));

td = new wxTextEntryDialog(this, wxT("Enter the number of dimensions: "), wxGetTextFromUserPromptStr, wxT("2"));
td->ShowModal();
SetMenuBar(menuBar);
str = td->GetValue();
n = wxAtoi(str);

mainBox = new wxBoxSizer(wxVERTICAL);
MainPanel = new wxPanel(this, wxID_ANY);
matrix = new wxStaticBoxSizer(wxVERTICAL, MainPanel, "Matrix: ");
cells = new wxFlexGridSizer(0, n, 0, 0);
subBox = new wxBoxSizer(wxVERTICAL);

for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
numbers[i][j] = new wxTextCtrl(MainPanel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(40,20), 0, wxDefaultValidator, _T("ID_TextCtrl"));
cells->Add(numbers[i][j], 0, wxALL | wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
}

matrix->Add(cells, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 2);
subBox->Add(matrix, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);

wxBoxSizer *enb = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *rnb = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *bc = new wxBoxSizer(wxHORIZONTAL);

Enter = new wxButton(MainPanel, ID_Calculate, _("Calculate"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_Calculate"));
Reset = new wxButton(MainPanel, ID_Reset, _("Reset"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_Reset"));
enb->Add(Enter, 1, wxALL |wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
rnb->Add(Reset, 1, wxALL |wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
bc->Add(enb, 0, wxALL | wxEXPAND);
bc->Add(rnb, 0, wxALL | wxEXPAND);
subBox->Add(bc, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL);
MainPanel->SetSizer(subBox);
mainBox->Add(MainPanel);
mainBox->Fit(this);
this->SetSizer(mainBox);
CentreOnScreen();
}

void determinantsFrame::OnReset(wxCommandEvent &event)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
numbers[i][j]->SetLabel(wxEmptyString);
}

void determinantsFrame::OnCalculateClick(wxCommandEvent &event)
{
double elem[MAX][MAX], det;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
elem[i][j] = static_cast<double>(wxAtoi(numbers[i][j]->GetValue()));

det = determinant(elem, n);
wxMessageBox(wxString::Format(wxT("%.2f"),det));
}

double determinantsFrame::determinant(double matrix[MAX][MAX], int order)
{
double det = 0, temp[MAX][MAX]; int row, col;

if (order == 1)
return matrix[0][0];
else if (order == 2)
return ((matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]));
else
{
for (int r = 0; r < order; r++)
{
col = 0; row = 0;
for (int i = 1; i < order; i++)
{
for (int j = 0; j < order; j++)
{
if (j == r)
continue;

temp[row][col] = matrix[i][j];
col++;

if (col == order - 1)
col = 0;
}
row++;
}
det = det + (matrix[0][r] * pow(-1, r) * determinant(temp, order - 1));
}
return det;
}
}

最佳答案

您不能使用 wxNewId() 这是一个运行时函数来初始化编译时事件表宏中使用的 ID。要么使 ID 成为真正的编译时常量,要么使用 Bind() 而不是事件表来在运行时连接处理程序。

顺便说一句,从 OnClose() 调用 exit() 是完全错误的。我强烈建议查看 wxWidgets 示例以了解事情通常是如何完成的。更好的方法是阅读文档以了解他们为什么这样做,但这可能是下一步。

关于c++ - wxButton 不会触发事件。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26216651/

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