skip to Main Content

I am following a tutorial to learn how to create simple GUIs with C++. (Video from the tutorial series I am using here(https://www.youtube.com/watch?v=_2FMt_0wVMM&list=PLFk1_lkqT8MbVOcwEppCPfjGOGhLvcf9G&index=4).

For this I am using Visual Studio 2022

Here is the code for the mainframe.

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

MainFrame::MainFrame(const wxString& title) : wxFrame(nullptr, wxID_ANY, title) {
    wxPanel* panel = new wxPanel(this);

    wxButton* button = new wxButton(this, wxID_ANY, "Button", wxPoint(150, 50), wxSize(100, 35));

    wxCheckBox* checkBox = new wxCheckBox(panel, wxID_ANY, "CheckBox", wxPoint(550, 55));

    wxStaticText* staticText = new wxStaticText(panel, wxID_ANY, "Static Text - NOT editable", wxPoint(120, 150));
}

The code runs 100% fine, however when my GUI pops up when I run on debug, the GUI only displays the button, but not the text or the checkbox. No checkbox or static text to be seen.

Someone please help I’m learning this for a school project and I really want to get on with learning all this.

2

Answers


  1. Chosen as BEST ANSWER

    Easy fix, I switched the wxPanel constructor for this:

    wxPanel* panel = new wxPanel(
        this,
        wxID_ANY,
        wxDefaultPosition,
        {1000, 1000}
    );
    

    And changed the pointer on the wxButton from 'this' to 'panel' and now everything appears as intended. Here is the new code:

    #include "MainFrame.h"
    #include <wx/wx.h>
    
    
    MainFrame::MainFrame(const wxString& title) : wxFrame(nullptr, wxID_ANY, title) {
        wxPanel* panel = new wxPanel(
            this,
            wxID_ANY,
            wxDefaultPosition,
            { 2000, 2000 }
        );
    
        wxButton* button = new wxButton(panel, wxID_ANY, "Button", wxPoint(150, 50), wxSize(120, 50));
    
        wxCheckBox* checkBox = new wxCheckBox(panel, wxID_ANY, "CheckBox", wxPoint(550, 55));
    
        wxStaticText* staticText = new wxStaticText(panel, wxID_ANY, "Static Text - NOT editable", wxPoint(120, 150));
    }
    

    Thank you for your help everyone, much appreciated. :)


  2. You need to make the panel bigger in order for the controls you place in it to be seen. You currently use mxDefaultSize, which is pretty small, as can be seen in the top left corner of your picture.

    The constructor you use is:

    wxPanel::wxPanel(
        wxWindow* parent,
        wxWindowID id = wxID_ANY,
        const wxPoint& pos = wxDefaultPosition,
        const wxSize& size = wxDefaultSize,       // <- set this to something bigger
        long style = wxTAB_TRAVERSAL,
        const wxString& name = wxPanelNameStr 
    )
    

    Example:

    wxPanel* panel = new wxPanel(
        this,
        wxID_ANY,
        wxDefaultPosition,
        {1000, 1000}
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search