C++ wxWidgets:创建小部件后设置字体
C++ wxWidgets: Set Font After Widget Creation
所以基本上我发现你只能在创建小部件之前设置字体,否则字体不会改变,但我需要在创建元素后更改字体,我该怎么做?
void MyMain::makeBtnPanel()
{
btnPanel = new wxPanel(this, wxID_ANY);
wxGridSizer* grid = new wxGridSizer(5, 4, 2, 2);
// here is hidden code that is adding widgets to grid
btnPanel->SetSizer(grid);
sizer->Add(btnPanel, 5, wxEXPAND);
}
void MyMain::styleBtns()
{
wxFont mainBtnFont(
16,
wxFONTFAMILY_DEFAULT,
wxFONTSTYLE_NORMAL,
wxFONTWEIGHT_EXTRALIGHT
);
btnPanel->SetFont(mainBtnFont);
}
styleBtns
函数不起作用,但如果我在 btnPanel
创建后立即设置字体(在 makeBtnPanel
函数的第二行),字体将被设置。
class构造函数:
MyMain::MyMain()
: wxFrame(
NULL,
wxID_ANY,
"Calculator",
wxDefaultPosition,
wxSize(322, 392)
)
{
sizer = new wxBoxSizer(wxVERTICAL);
SetSizer(sizer);
makeDisplayPanel();
makeBtnPanel();
styleBtns();
setupMainFrame();
}
天哪,我太笨了。我无法更改字体,因为我将字体设置为空变量。我写了 else if
而不是 if
并且代码没有执行,它没有设置这些变量。
所以基本上我发现你只能在创建小部件之前设置字体,否则字体不会改变,但我需要在创建元素后更改字体,我该怎么做?
void MyMain::makeBtnPanel()
{
btnPanel = new wxPanel(this, wxID_ANY);
wxGridSizer* grid = new wxGridSizer(5, 4, 2, 2);
// here is hidden code that is adding widgets to grid
btnPanel->SetSizer(grid);
sizer->Add(btnPanel, 5, wxEXPAND);
}
void MyMain::styleBtns()
{
wxFont mainBtnFont(
16,
wxFONTFAMILY_DEFAULT,
wxFONTSTYLE_NORMAL,
wxFONTWEIGHT_EXTRALIGHT
);
btnPanel->SetFont(mainBtnFont);
}
styleBtns
函数不起作用,但如果我在 btnPanel
创建后立即设置字体(在 makeBtnPanel
函数的第二行),字体将被设置。
class构造函数:
MyMain::MyMain()
: wxFrame(
NULL,
wxID_ANY,
"Calculator",
wxDefaultPosition,
wxSize(322, 392)
)
{
sizer = new wxBoxSizer(wxVERTICAL);
SetSizer(sizer);
makeDisplayPanel();
makeBtnPanel();
styleBtns();
setupMainFrame();
}
天哪,我太笨了。我无法更改字体,因为我将字体设置为空变量。我写了 else if
而不是 if
并且代码没有执行,它没有设置这些变量。