首页 > 解决方案 > wxWidgets。像在 CMake GUI 中一样调整 wxFrame 中的子区域大小

问题描述

我是 wxWidgets 的新手,所以我试图让窗口像在 CMake GUI 中一样。如何将该区域划分为两个可自行调整大小的区域:

CMake 可调整大小的线

我正在使用 linux 进行开发和 cmake。我没有使用任何 UI 编辑器

PS:对不起我的英语,这不是我的母语:o

标签: c++cmakewxwidgetscmake-gui

解决方案


在 CMake GUI 中创建元素扩展的框架的方法是将 wxSplitterWindow 与 sizer 一起使用,并使用 sizer flags 构造函数中的比例来指定每个项目应该扩展多少。

这是一个简短的示例:

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include <wx/splitter.h>

class MyFrame: public wxFrame
{
    public:
        MyFrame();
};

MyFrame::MyFrame()
        :wxFrame(NULL, wxID_ANY, "CMake style frame", wxDefaultPosition,
                 wxSize(600, 400))
{
    wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY,
                                                      wxDefaultPosition, wxDefaultSize,
                                                      wxSP_LIVE_UPDATE);

    wxPanel* top = new wxPanel(splitter,wxID_ANY);

    wxStaticText* statText1 = new wxStaticText(top, wxID_ANY,
                                               "Where is the source");
    wxTextCtrl* textCtrl1 = new wxTextCtrl(top, wxID_ANY, wxEmptyString);
    wxButton* button1 = new wxButton(top, wxID_ANY, "Browse Source...");
    wxBoxSizer* sizer2 = new wxBoxSizer(wxHORIZONTAL);
    sizer2->Add(statText1,wxSizerFlags(0).Centre());
    sizer2->Add(textCtrl1,wxSizerFlags(1).Border(wxLEFT));
    sizer2->Add(button1,wxSizerFlags(0).Border(wxLEFT));

    wxStaticText* statText2 = new wxStaticText(top ,wxID_ANY, "Where to build the binaries:");
    wxStaticText* statText3 = new wxStaticText(top, wxID_ANY, "Search:");

    wxTextCtrl* textCtrl2 = new wxTextCtrl(top, wxID_ANY, wxEmptyString,
                                           wxDefaultPosition, wxDefaultSize,
                                           wxTE_MULTILINE|wxTE_DONTWRAP);
    wxStaticText* statText4 = new wxStaticText(top, wxID_ANY, "Press Configure:");
    wxButton* button2 = new wxButton(top, wxID_ANY, "Configure");

    wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);

    // Now Add the items to the backfround panel's sizer:
    topSizer->Add(sizer2,wxSizerFlags(0).Expand().DoubleBorder(wxALL));
    topSizer->Add(statText2,wxSizerFlags(0).DoubleBorder(wxLEFT|wxRIGHT|wxBOTTOM));
    topSizer->Add(statText3,wxSizerFlags(0).DoubleBorder(wxLEFT|wxRIGHT|wxBOTTOM));

    // The next item is added with proportion 1 so that it will expand in size.
    topSizer->Add(textCtrl2,
                  wxSizerFlags(1).Expand().DoubleBorder(wxLEFT|wxRIGHT|wxBOTTOM));
    topSizer->Add(statText4,
                  wxSizerFlags(0).Center().DoubleBorder(wxLEFT|wxRIGHT|wxBOTTOM));
    topSizer->Add(button2,wxSizerFlags(0).DoubleBorder(wxLEFT|wxRIGHT));

    top->SetSizer(topSizer);


    wxPanel* botton = new wxPanel(splitter,wxID_ANY);
    wxTextCtrl* textCtrl3 = new wxTextCtrl(botton, wxID_ANY, wxEmptyString,
                                           wxDefaultPosition, wxDefaultSize,
                                           wxTE_MULTILINE|wxTE_DONTWRAP);
    wxBoxSizer* botomSizer = new wxBoxSizer(wxVERTICAL);

    botomSizer->Add(textCtrl3,
                    wxSizerFlags(1).Expand().DoubleBorder(wxLEFT|wxRIGHT|wxBOTTOM));
    botton->SetSizer(botomSizer);

    // Add the top and bottom parts to the splitter, and set the sash gravity to
    // .5 so that both halfs will expand equally when the frame is resized.
    splitter->SplitHorizontally(top,botton);
    splitter->SetSashGravity(.5);

    Layout();
}

class MyApp : public wxApp
{
    public:
        virtual bool OnInit()
        {
            ::wxInitAllImageHandlers();
            MyFrame* frame = new MyFrame();
            frame->Show();
            return true;
        }
};

wxIMPLEMENT_APP(MyApp);

在 Windows 上,这看起来像这样:

在此处输入图像描述

我没有从 CMake GUI 框架中复制每个小部件,但我认为那里有足够的内容可以让您了解正在发生的事情。


推荐阅读