Search code examples
c++user-interfacewxwidgets

How to create a widgets (Buttons) form Derived Class


I' am newbie, im styding wxwidgets and c++, in this topic i want ask How to create a widgets (Buttons) form Derived Class ( APMainFrame that inherit MainFrame class)

Code MainFrame GUI:

MainFrame::MainFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxFrame( parent, id, title, pos, size, style )
{
    this->SetSizeHints( wxDefaultSize, wxDefaultSize );
    this->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWFRAME ) );
    .....
    this->SetMenuBar( m_menubar1 );
    this->Centre( wxBOTH );
}

and APMainFrame.h code:

class APMainFrame : public MainFrame
{
    public:
        /** Constructor */
        APMainFrame( wxWindow* parent );
    //// end generated class members
        wxButton* HelloWorld; // here i wanna create function and button for GUI
        void OnExit(wxCommandEvent& event);
private:
        DECLARE_EVENT_TABLE()
};

enum
{
    BUTTON_Hello = wxID_HIGHEST + 1 
};

and file APMainFrame.cpp:

BEGIN_EVENT_TABLE(APMainFrame, MainFrame)
EVT_BUTTON(BUTTON_Hello, APMainFrame::OnExit) 
END_EVENT_TABLE() // The button is pressed

APMainFrame::APMainFrame( wxWindow* parent )
:
MainFrame( parent )
{
    HelloWorld = new wxButton(this, BUTTON_Hello, _T("Hello World"),
        // shows a button on this window
        wxDefaultPosition, wxDefaultSize, 0); 

}

void APMainFrame::OnExit(wxCommandEvent& event)
{
    Close(TRUE);
}

I just wanna create Widgets form Drived class. Thanks so much.


Solution

  • You need to add a panel (wxPanel) and a sizer to your wxFrame derived frame.

    You have created your button, but you also need to add it to the frame so it knows to render it.

    Thus, like so:

    MainFrame( parent )
    {
        wxPanel* panel = new wxPanel(this, wxID_ANY);
        wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
        panel->SetSizer(sizer);
        HelloWorld = new wxButton(this, BUTTON_Hello, _T("Hello World"),
            // shows a button on this window
            wxDefaultPosition, wxDefaultSize, 0); 
    
        sizer->Add(HelloWorld);
    }
    

    Additional helpful resources: