Page 1 of 1

Why does SetFont destroy the sizer's layout??

Posted: Wed Aug 05, 2009 1:03 pm
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?

Posted: Wed Aug 05, 2009 2:21 pm
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.

Posted: Wed Aug 05, 2009 2:35 pm
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(...)).

Posted: Wed Aug 05, 2009 3:04 pm
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().

Posted: Wed Aug 05, 2009 5:24 pm
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

Posted: Wed Aug 05, 2009 5:42 pm
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.

Posted: Thu Aug 20, 2009 7:05 pm
by Sunsawe
thank you!
that was the problem.