Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 自定义wxWidgets小部件中相邻的两个按钮_C++_Wxwidgets - Fatal编程技术网

C++ 自定义wxWidgets小部件中相邻的两个按钮

C++ 自定义wxWidgets小部件中相邻的两个按钮,c++,wxwidgets,C++,Wxwidgets,我试图实现一个自定义的wxWidgets小部件,其中一个减号按钮和一个加号按钮相邻放置 为了实现这一点,我让我的自定义小部件类继承自wxPanel,并使用水平wxBoxSizer放置两个按钮: #include <wx/wx.h> class CustomWidget : public wxPanel{ private: wxButton* m_minusButton; wxButton* m_plusButton; public: CustomWidge

我试图实现一个自定义的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);
    }
};
如果编译并运行此程序,结果可以在下图中看到:

理想的结果是,一个减号按钮和一个加号按钮相邻绘制。但是,加号按钮似乎绘制在减号按钮的顶部


我如何修复按钮,使它们彼此相邻地绘制?

我认为这仅仅是因为您的
CustomWidget
从未获得调整大小事件,因此从未布局过,因此只需显式地调用
Layout()
(在其ctor末尾或创建之后)就可以解决问题

FWIW通常不会出现这个问题,因为小部件是由大小调整器定位的,当它们放置到位时会得到一个调整大小事件,但当您在固定位置创建这个小部件时,它不会由大小调整器管理,它永远不会得到任何大小事件

最后,我不知道为什么需要加号/减号按钮,但如果要将它们与任何类似listbox的控件结合使用,您可能会对最新的git master中的可用性感兴趣,它试图为所有主要平台实现本机外观。

Calling Layout()在构造函数中,将自定义控件放在垂直大小调整器中解决了这个问题。谢谢你的帮助!
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);