首页 > 解决方案 > 如何打开特定窗口到 wxWidgets 上的特定按钮?

问题描述

我来这里之前做了我的研究,找不到任何有用的东西。请告诉我,因为我是 wxWidgets 和 C++ GUI 的新手。我创建了一个 3x3 网格,每个按钮都有自己的用途。单击时,一些按钮应打开一个至少包含按钮文本内容的新页面,而其他按钮应将您定向到按钮上的网站。我尝试在事件处理程序中创建一个新窗口,但即使我使用Show(); 我有一个 cApp 类,它是应用程序的启动器,cMain 是图形界面实现,也没有任何显示。我将只显示 cMain。

cMAIN.H

#pragma once
//Graphical interface component
#include "wx/wx.h"

class cMain : public wxFrame
{
public:
    cMain();
    ~cMain();
public:
    int nFieldWidth = 3;
    int nFieldHeight = 3;
    wxButton** btn;
    
    void OnButtonClicked0(wxCommandEvent& evt);
    void OnButtonClicked1(wxCommandEvent &evt);
    void OnButtonClicked2(wxCommandEvent &evt);
    void OnButtonClicked3(wxCommandEvent &evt);
    void OnButtonClicked4(wxCommandEvent &evt);
    void OnButtonClicked5(wxCommandEvent &evt);
    void OnButtonClicked6(wxCommandEvent &evt);
    void OnButtonClicked7(wxCommandEvent &evt);
    void OnButtonClicked8(wxCommandEvent &evt);
    wxDECLARE_EVENT_TABLE();
};

cMAIN.CPP

#include "cMain.h"
#include "cApp.h"
#include <string>
wxBEGIN_EVENT_TABLE(cMain, wxFrame)
EVT_BUTTON(0, OnButtonClicked0)
EVT_BUTTON(1, OnButtonClicked1)
EVT_BUTTON(2, OnButtonClicked2)
EVT_BUTTON(3, OnButtonClicked3)
EVT_BUTTON(4, OnButtonClicked4)
EVT_BUTTON(5, OnButtonClicked5)
EVT_BUTTON(6, OnButtonClicked6)
EVT_BUTTON(7, OnButtonClicked7)
EVT_BUTTON(8, OnButtonClicked8)
wxEND_EVENT_TABLE();


cMain::cMain() : wxFrame(nullptr, wxID_ANY, "3x3Grid", wxPoint(30,30), wxSize(800,600))//overriding the frame
{
    btn = new wxButton * [nFieldWidth * nFieldHeight]; // creating an array of buttons of size nFW * nFH
    wxGridSizer* grid = new wxGridSizer(nFieldWidth, nFieldHeight, 10, 10); //creating new sizer grid

    wxFont font(24, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false);

    for (int x = 0; x < nFieldWidth; x++)
    {
        for (int y = 0; y < nFieldHeight; y++)
        {
            btn[y * nFieldWidth + x] = new wxButton(this, 10000 + (y * nFieldWidth + x), std::to_string(y * nFieldWidth + x)); //sets button[index] = a new button
            btn[y * nFieldWidth + x]->SetFont(font); //set font
            grid->Add(btn[y * nFieldWidth + x], 1, wxEXPAND | wxALL); //adds button to grid
        }
    }
    btn[0]->SetLabel("Input Data");
    btn[1]->SetLabel("Read Data from File");
    btn[2]->SetLabel("Open www.website.com");
    btn[3]->SetLabel("Lookup Data");
    btn[4]->SetLabel("Open website.com");
    btn[5]->SetLabel("Open https://website.com");
    btn[6]->SetLabel("Store Data to File");
    btn[7]->SetLabel("Open https://website.com");
    btn[8]->SetLabel("Help");

    this->SetSizer(grid);
    grid->Layout();
    
}


cMain::~cMain()
{
    delete[]btn;
}

void cMain::OnButtonClicked0(wxCommandEvent& evt)
{
    cMain* inputwindow = new cMain();
    inputwindow->Show();
}

void cMain::OnButtonClicked1(wxCommandEvent& evt)
{
}
void cMain::OnButtonClicked2(wxCommandEvent& evt)
{
}
void cMain::OnButtonClicked3(wxCommandEvent& evt)
{
}
void cMain::OnButtonClicked4(wxCommandEvent& evt)
{
}
void cMain::OnButtonClicked5(wxCommandEvent& evt)
{
}
void cMain::OnButtonClicked6(wxCommandEvent& evt)
{
}
void cMain::OnButtonClicked7(wxCommandEvent& evt)
{
}
void cMain::OnButtonClicked8(wxCommandEvent& evt)
{
}

标签: c++user-interfacevisual-c++event-handlingwxwidgets

解决方案


按钮 ID 和 EVT_BUTTON(id) 不匹配。例如按钮 0 的 id 是 10000,而不是 0。


推荐阅读