Ben Nguyen
Ben Nguyen

Reputation: 61

Instantiating a wxWidget Panel window that was generated using wxFormBuilder

I have a Win32 project (Windows Subsystem), and a Panel I created in wxFormBuilder. The problem is that the Panel requires a parent window when it's instantiated, but I don't have any Window handle for it.

As a test, I created a Frame type window, which does not require any parameters when instantiating, this works and displays a window/Frame when the object is created.

Since I prefer the Dialog type window (not a frame), how can I get the Panel to display ?

#include <wx/wx.h>
#include "gui.h"

class MyApp : public wxApp
{
public:
    bool OnInit() override;
};

wxIMPLEMENT_APP(MyApp);

bool MyApp::OnInit()
{
    MyPanel* mp = new MyPanel(NULL); //<---RUNTIME ERROR: "can't create wxWindow without parent"
    mp->Show(true);
    return true;
}

where the generated gui information looks like:

class MyPanel : public wxPanel
{
private:

protected:
    wxButton* m_bnInit;

public:
    MyPanel(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize(500, 300), long style = wxTAB_TRAVERSAL, const wxString& name = wxEmptyString);
    ~MyPanel();

};

Upvotes: 0

Views: 85

Answers (2)

Igor
Igor

Reputation: 6255

As already mentioned wxPanel is NOT a top-level window, just a container.

Since you want to use a dialog - why not make a wxDialog and put a wxPanel on top of it.

It is actually a preferred way of making the GUI with wxWidgets. wxPanel will let you have TAB and SHIFT+TAB for free (no need to handle key event specifically).

And if you put everything in the sizer (as you should), the dialog will be sized nicely on every platform according to its content.

Upvotes: 0

Ben Nguyen
Ben Nguyen

Reputation: 61

Doesn't seem like there's a way to just display the panel, it needs to have a frame. Not sure if there's a better way but this worked:

bool MyApp::OnInit()
{
    wxFrame* parent = new wxFrame(NULL, wxID_ANY, "Parent Frame");
    MyPanel* myWinPanel = new MyPanel(parent);
    parent->Show(true);
    myWinPanel->Show(true);
    return true;
}

Upvotes: 0

Related Questions