Layout using sizer not function as expected 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
quanzhuo
In need of some credit
In need of some credit
Posts: 7
Joined: Tue Nov 10, 2020 9:58 am

Layout using sizer not function as expected

Post by quanzhuo » Thu May 06, 2021 1:49 am

Hi, guys:

I create a simple wxFrame based App, place a wxPanel on wxFrame, and then add a sizer and a button. the result layout is
Image
The frame is much larger than the button. How can I make the frame fit to the button's size ?
What I expected is something like this:
Image

I use wxWidgets 3.15, build with cmake

the code :

Code: Select all

#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif


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

class MyFrame:public wxFrame {
public:
    MyFrame(const wxString &title);
};


MyFrame::MyFrame(const wxString &title): wxFrame(nullptr, wxID_ANY, title)
{
    wxPanel *panel = new wxPanel(this, wxID_ANY);
    wxButton *btn = new wxButton(panel, wxID_ANY, "Button");
    wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
    sizer->Add(btn, wxSizerFlags(0).Align(wxALIGN_CENTER_HORIZONTAL).Border());

    panel->SetSizer(sizer);
    sizer->SetSizeHints(this);
}

bool MyApp::OnInit() {
    wxFrame *frame = new MyFrame("MyFrame");
    frame->Show();
    return true;
}

wxIMPLEMENT_APP(MyApp);

User avatar
doublemax
Moderator
Moderator
Posts: 16022
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Layout using sizer not function as expected

Post by doublemax » Thu May 06, 2021 8:00 am

That code used to work until 3.1.4, so it's probably a bug.

Instead of

Code: Select all

sizer->SetSizeHints(this);
use this for now (this is what SetSizeHints() used to do):

Code: Select all

const wxSize clientSize = sizer->ComputeFittingClientSize(this);
this->SetMinClientSize(clientSize);
this->SetClientSize(clientSize);
Use the source, Luke!

quanzhuo
In need of some credit
In need of some credit
Posts: 7
Joined: Tue Nov 10, 2020 9:58 am

Re: Layout using sizer not function as expected

Post by quanzhuo » Thu May 06, 2021 8:38 am

doublemax wrote:
Thu May 06, 2021 8:00 am
That code used to work until 3.1.4, so it's probably a bug.

Instead of

Code: Select all

sizer->SetSizeHints(this);
use this for now (this is what SetSizeHints() used to do):

Code: Select all

const wxSize clientSize = sizer->ComputeFittingClientSize(this);
this->SetMinClientSize(clientSize);
this->SetClientSize(clientSize);
oh oh oh
You solved the problem that confused me for serval days after I move to wxWidgets 3.1.5.
So, as you said, the bug has existed for a long time ?
Will the commutity update the official sample demo project ?

Thank you so much !

User avatar
doublemax
Moderator
Moderator
Posts: 16022
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Layout using sizer not function as expected

Post by doublemax » Thu May 06, 2021 8:54 am

quanzhuo wrote:
Thu May 06, 2021 8:38 am
So, as you said, the bug has existed for a long time ?
Will the commutity update the official sample demo project ?
No, it's a new bug, your code still worked in 3.1.4

I opened a bug report here: https://trac.wxwidgets.org/ticket/19170
Use the source, Luke!

Post Reply