【发布时间】:2015-09-30 06:27:21
【问题描述】:
我正在尝试实现一个自定义 wxWidgets 小部件,其中减号按钮和加号按钮彼此相邻放置。
为了实现这一点,我让我的自定义小部件类继承自 wxPanel,并使用水平 wxBoxSizer 放置两个按钮:
#include <wx/wx.h>
class CustomWidget : public wxPanel{
private:
wxButton* m_minusButton;
wxButton* m_plusButton;
public:
CustomWidget(wxWindow *parent, const wxPoint& pos): wxPanel(parent, wxID_ANY, pos, wxSize(-1, -1), wxBORDER_NONE){
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
m_minusButton = new wxButton(this, wxID_ANY, wxT("-"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT );
m_plusButton = new wxButton(this, wxID_ANY, wxT("+"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT );
hbox->Add(m_minusButton, 1, wxALL, 5);
hbox->Add(m_plusButton, 1, wxALL, 5);
hbox->SetSizeHints(this);
this->SetSizer(hbox);
}
};
我的应用程序分为左窗格和右窗格。我将自定义小部件放在右窗格中。这是我的应用程序的一个大大简化的版本:
class TestFrame: public wxFrame{
wxPanel *m_lp;
wxPanel *m_rp;
public:
TestFrame(): wxFrame(NULL, wxID_ANY, "Title", wxDefaultPosition, wxSize(400,400)){
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
m_lp = new wxPanel(this,-1, wxPoint(-1, -1), wxSize(-1, -1), wxBORDER_SUNKEN);
m_rp = new wxPanel(this,-1, wxPoint(-1, -1), wxSize(-1, -1), wxBORDER_SUNKEN);
hbox->Add(m_lp, 1, wxEXPAND | wxALL, 5);
hbox->Add(m_rp, 1, wxEXPAND | wxALL, 5);
this->SetSizer(hbox);
new CustomWidget(m_rp, wxPoint(50,100));
}
};
class TestApp: public wxApp{
public:
virtual bool OnInit() {
TestFrame *frame = new TestFrame();
frame->Show( true );
return true;
}
};
wxIMPLEMENT_APP(TestApp);
如果你编译运行这个程序,结果如下图所示:
http://i.imgur.com/JcRJJKi.png
期望的结果是一个减号按钮和一个加号按钮被绘制在彼此旁边。但是,加号按钮似乎绘制在减号按钮的顶部。
如何解决,使按钮彼此相邻绘制?
【问题讨论】: