Why does SetFont destroy the sizer's layout?? 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
Sunsawe
Experienced Solver
Experienced Solver
Posts: 54
Joined: Tue Jan 30, 2007 5:04 pm

Why does SetFont destroy the sizer's layout??

Post by Sunsawe »

Hi,

I really do not understand what's happening there.
I have a piece of code which is working fine:

Code: Select all

fgs = new wxFlexGridSizer(0,2,1,1);
fgs->AddGrowableCol(1);

fgs->Add( (new wxStaticText(page,wxID_ANY,wxT("Name :")))
         //->SetFont(wxFont(16,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD))
);

fgs->Add( new wxStaticText(page,wxID_ANY,wxT("Test Failure")),1,wxEXPAND,0);

if(condition)
{
fgs->Add( (new wxStaticText(page,wxID_ANY,wxT("Level :")))
    //->SetFont(wxFont(16,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL))
);
fgs->Add( new wxStaticText(page,wxID_ANY,wxT("Critical Failure")),1,wxEXPAND,0);
}

page->SetSizer(fgs);
page->Layout();
The elements are appearing nicely according to the sizer.
But if I uncomment the SetFont lines (specially the second), all concerned elements are appearing on top of each others at the case 0,0.

Can someone explain what is happening?
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Maybe try setting the font on the static text itself, not on the returned sizer item returned by ->Add(). I'm not sure setting font makes sense on a wxSizerItem object, actually most of the time you don't use that object directly at all.
"Keyboard not detected. Press F1 to continue"
-- Windows
Sunsawe
Experienced Solver
Experienced Solver
Posts: 54
Joined: Tue Jan 30, 2007 5:04 pm

Post by Sunsawe »

Well, actually, that's what I do.

If you look at the brackets, you'll notice that the setFont is done inside the sizer->Add() call and so, on the object returned by (new wxStaticText(...)).
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

Code: Select all

If you look at the brackets, you'll notice that the setFont is done inside the sizer->Add() call and so, on the object returned by (new wxStaticText(...)).
well, the return value from "new wxStaticText(...)" is the pointer to the object, but the return value from "(new wxStaticText(...))->SetFont()" is a bool.

So you're in fact calling a totally different version of the overloaded wxSizer::Add().
Use the source, Luke!
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

doublemax wrote:

Code: Select all

If you look at the brackets, you'll notice that the setFont is done inside the sizer->Add() call and so, on the object returned by (new wxStaticText(...)).
well, the return value from "new wxStaticText(...)" is the pointer to the object, but the return value from "(new wxStaticText(...))->SetFont()" is a bool.

So you're in fact calling a totally different version of the overloaded wxSizer::Add().
I actually see a void? But anyway, yes that's the issue

http://docs.wxwidgets.org/stable/wx_wxw ... dowsetfont
"Keyboard not detected. Press F1 to continue"
-- Windows
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

the documentation is wrong, it's a bool ;)

Code: Select all

// set/retrieve the font for the window (SetFont() returns true if the
// font really changed)
virtual bool SetFont(const wxFont& font) = 0;
If it was a void, the code probably didn't compile and the error could have been detected immediately.
Use the source, Luke!
Sunsawe
Experienced Solver
Experienced Solver
Posts: 54
Joined: Tue Jan 30, 2007 5:04 pm

Post by Sunsawe »

thank you!
that was the problem.
Post Reply