adding panels to a seizer in a loop Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
JFG
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Dec 09, 2022 5:16 pm

adding panels to a seizer in a loop

Post by JFG »

Hello there,

am new to wxWidgets so... yea :>

I kind of started of with this video for seizers
https://youtu.be/kPB5Y6ef9dw?t=135
Around this timestamp it is said, that the "this" on creating a panel like
wxPanel* p1 = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(20 , 20));
transfers ownership to the framework.

Where am not sure about is, what happens with seizers, since they don´t have a parent argument.

What I´m trying is, to crate an own class deriving from wxPanel which puts two panels in a horizontal seizer which trough a loop in the constructor
get´s put in a vertical seizer to create an arrangement like
arrangement.png
arrangement.png (14.62 KiB) Viewed 3257 times

What I have is, that the last created panel is displayed in left upepr corner. I already read, it´s probably a parenting issue, but I can´t figure it.
Plus I don´t know if this attempt of mine even works.

best regards,
Justus

.h

Code: Select all

#ifndef STCRT_MENU_BASE_H
#define STCRT_MENU_BASE_H

#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/stattext.h>

class Strct_Menu_Base_Ui : public wxPanel
{
    public:
    Strct_Menu_Base_Ui(wxWindow* parent);

    protected:

    private:
    
};

#endif
.cpp

Code: Select all

#include "Strct_Menu_Base_Ui.h"

#define AMT_OF_MENU_ITEMS   3

Strct_Menu_Base_Ui::Strct_Menu_Base_Ui(wxWindow* parent) : wxPanel(parent, wxID_ANY, wxDefaultPosition, wxSize(100 , 100))
{
    wxBoxSizer* seizerV = new wxBoxSizer(wxVERTICAL);
    
    for(int i=0; i<AMT_OF_MENU_ITEMS; i++)
    {
        
        wxBoxSizer* sizerHor = new wxBoxSizer(wxHORIZONTAL);
        seizerV->Add(sizerHor, 1, wxEXPAND | wxALL  );
        
        std::unique_ptr<wxBoxSizer> sizerHor = std::make_unique<wxBoxSizer>(wxHORIZONTAL);
        seizerV->Add(sizerHor.get(), 1, wxEXPAND | wxALL);
        seizHorVec.emplace_back(std::move(sizerHor));

        std::unique_ptr<wxPanel> p1 = std::make_unique<wxPanel>(this, wxID_ANY, wxDefaultPosition, wxSize(20 , 20));
        p1->SetBackgroundColour(wxColour(255, 0, 0));
        sizerHor->Add(p1.get(), 1, wxEXPAND | wxALL);
    }
    

}
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: adding panels to a seizer in a loop

Post by ONEEYEMAN »

Hi,
You are missing a call to Layout() after the loop ends.

Thank you.
JFG
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Dec 09, 2022 5:16 pm

Re: adding panels to a seizer in a loop

Post by JFG »

I tried
this->Layout();
and
this->GetPartent->Layout();

output for both is the same
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: adding panels to a seizer in a loop

Post by PB »

Firstly, you did not post the actual code, the code would not compile as it has two variables declared at the same scope (sizerHor).

Secondly, do not use smart pointers for windows or sizers. The lifetime of windows and sizers is managed by wxWidgets, they are destroyed when needed.

Thirdly, your code misses a sizer assignment call,something like

Code: Select all

SetSizer(seizerV)
in the Strct_Menu_Base_Ui constructor. Such call makes the sizer work correctly and also lets wxWidgets to destroy it when needed. The sizers do not have parent, they have owners (here Strct_Menu_Base_Ui for seizerV).
JFG
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Dec 09, 2022 5:16 pm

Re: adding panels to a seizer in a loop

Post by JFG »

... sorry, yea, am working via a remote VM and something went wrong with the versions...

But the SetSeizer(); was the key.

Thanks!
Post Reply