使用 CodeBlock 在 WxSmith gui 编程中向 wxPanel 添加布局后程序崩溃

Program crashed after adding a layout to a wxPanel in WxSmith gui programming with CodeBlock

我正在使用 CodeBlock 中的 WxSmith 学习图形用户界面编程 IDE。我按照以下步骤创建了一个示例项目

  1. 使用 WxSmith 版本 3.0 创建一个 WxSmith 项目
  2. 在项目的默认主框架中添加一个3列的box sizer
  3. 向每列添加 2 个按钮和 1 个面板
  4. 将 WxBoxSizer 添加到面板
  5. 构建项目并 运行,构建成功但生成 运行 时间错误。

使用 GDB 时,出现以下错误。 启动程序:/home/dwft78/Documents/Applications/TEST/bin/Debug/TEST [使用 libthread_db 启用线程调试] 使用主机 libthread_db 库“/lib/x86_64-linux-gnu/libthread_db.so.1”.

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff79e2256 in wxWindowBase::InformFirstDirection(int, int, int) () from /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
(gdb) bt
#0  0x00007ffff79e2256 in wxWindowBase::InformFirstDirection(int, int, int) () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#1  0x00007ffff79b3d95 in wxSizerItem::InformFirstDirection(int, int, int) () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#2  0x00007ffff79b4500 in wxBoxSizer::RecalcSizes() () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#3  0x00007ffff79b263e in wxSizer::Layout() () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#4  0x00007ffff79e6956 in wxWindowBase::Layout() () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#5  0x00005555555658bf in TESTFrame::TESTFrame(wxWindow*, int) (this=0x555555835000, parent=0x0, id=-1) at /home/dwft78/Documents/Applications/TEST/TESTMain.cpp:101
#6  0x000055555556361b in TESTApp::OnInit() (this=0x5555557cc600) at /home/dwft78/Documents/Applications/TEST/TESTApp.cpp:27
#7  0x0000555555563c81 in wxAppConsoleBase::CallOnInit() (this=0x5555557cc600) at /usr/include/wx-3.0/wx/app.h:93
#8  0x00007ffff7205b3a in wxEntry(int&, wchar_t**) () at /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#9  0x000055555556357c in main(int, char**) (argc=1, argv=0x7fffffffdec8) at /home/dwft78/Documents/Applications/TEST/TESTApp.cpp:18
(gdb) 

我没有添加任何代码行,所有代码都是由 IDE 自己生成的。我正在使用 Code::block 版本 20.03。以下是为该项目生成的文件。

以下是项目 GUI 组件的屏幕截图

以下是为项目生成的 cpp 和头文件。

  1. TESTMain.cpp
 #include "TESTMain.h"
 #include <wx/msgdlg.h>
 
 //(*InternalHeaders(TESTFrame)
 #include <wx/intl.h>
 #include <wx/string.h>
 //*)
 
 //helper functions
 enum wxbuildinfoformat {
     short_f, long_f };
 
 wxString wxbuildinfo(wxbuildinfoformat format)
 {
     wxString wxbuild(wxVERSION_STRING);
 
     if (format == long_f )
     {
 #if defined(__WXMSW__)
         wxbuild << _T("-Windows");
 #elif defined(__UNIX__)
         wxbuild << _T("-Linux");
 #endif
 
 #if wxUSE_UNICODE
         wxbuild << _T("-Unicode build");
 #else
         wxbuild << _T("-ANSI build");
 #endif // wxUSE_UNICODE
     }
 
     return wxbuild;
 }
 
 //(*IdInit(TESTFrame)
 const long TESTFrame::ID_BUTTON1 = wxNewId();
 const long TESTFrame::ID_BUTTON2 = wxNewId();
 const long TESTFrame::ID_PANEL1 = wxNewId();
 const long TESTFrame::idMenuQuit = wxNewId();
 const long TESTFrame::idMenuAbout = wxNewId();
 const long TESTFrame::ID_STATUSBAR1 = wxNewId();
 //*)
 
 BEGIN_EVENT_TABLE(TESTFrame,wxFrame)
     //(*EventTable(TESTFrame)
     //*)
 END_EVENT_TABLE()
 
 TESTFrame::TESTFrame(wxWindow* parent,wxWindowID id)
 {
     //(*Initialize(TESTFrame)
     wxBoxSizer* BoxSizer1;
     wxBoxSizer* BoxSizer2;
     wxMenu* Menu1;
     wxMenu* Menu2;
     wxMenuBar* MenuBar1;
     wxMenuItem* MenuItem1;
     wxMenuItem* MenuItem2;
 
     Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
     SetClientSize(wxSize(914,512));
     BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
     Button1 = new wxButton(this, ID_BUTTON1, _("Label"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
     BoxSizer1->Add(Button1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
     Button2 = new wxButton(this, ID_BUTTON2, _("Label"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
     BoxSizer1->Add(Button2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
     Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxSize(268,236), wxTAB_TRAVERSAL, _T("ID_PANEL1"));
     BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
     Panel1->SetSizer(BoxSizer2);
     SetSizer(BoxSizer2);
     Layout();
     BoxSizer1->Add(Panel1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
     SetSizer(BoxSizer1);
     MenuBar1 = new wxMenuBar();
     Menu1 = new wxMenu();
     MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
     Menu1->Append(MenuItem1);
     MenuBar1->Append(Menu1, _("&File"));
     Menu2 = new wxMenu();
     MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
     Menu2->Append(MenuItem2);
     MenuBar1->Append(Menu2, _("Help"));
     SetMenuBar(MenuBar1);
     StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
     int __wxStatusBarWidths_1[1] = { -1 };
     int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
     StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
     StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
     SetStatusBar(StatusBar1);
     SetSizer(BoxSizer1);
     Layout();
 
     Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&TESTFrame::OnQuit);
     Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&TESTFrame::OnAbout);
     //*)
 }
 
 TESTFrame::~TESTFrame()
 {
     //(*Destroy(TESTFrame)
     //*)
 }
 
 void TESTFrame::OnQuit(wxCommandEvent& event)
 {
     Close();
 }
 
 void TESTFrame::OnAbout(wxCommandEvent& event)
 {
     wxString msg = wxbuildinfo(long_f);
     wxMessageBox(msg, _("Welcome to..."));
 }```

2. TESTMain.h

 ```#ifndef TESTMAIN_H
 #define TESTMAIN_H
 
 //(*Headers(TESTFrame)
 #include <wx/button.h>
 #include <wx/frame.h>
 #include <wx/menu.h>
 #include <wx/panel.h>
 #include <wx/sizer.h>
 #include <wx/statusbr.h>
 //*)
 
 class TESTFrame: public wxFrame
 {
     public:
 
         TESTFrame(wxWindow* parent,wxWindowID id = -1);
         virtual ~TESTFrame();
 
     private:
 
         //(*Handlers(TESTFrame)
         void OnQuit(wxCommandEvent& event);
         void OnAbout(wxCommandEvent& event);
         //*)
 
         //(*Identifiers(TESTFrame)
         static const long ID_BUTTON1;
         static const long ID_BUTTON2;
         static const long ID_PANEL1;
         static const long idMenuQuit;
         static const long idMenuAbout;
         static const long ID_STATUSBAR1;
         //*)
 
         //(*Declarations(TESTFrame)
         wxButton* Button1;
         wxButton* Button2;
         wxPanel* Panel1;
         wxStatusBar* StatusBar1;
         //*)
 
         DECLARE_EVENT_TABLE()
 };
 
 #endif // TESTMAIN_H```

3. TESTApp.h

 ```#ifndef TESTAPP_H
 #define TESTAPP_H
 
 #include <wx/app.h>
 
 class TESTApp : public wxApp
 {
     public:
         virtual bool OnInit();
 };
 
 #endif // TESTAPP_H```

4. TESTApp.cpp

 ```#include "wx_pch.h"
 #include "TESTApp.h"
 
 //(*AppHeaders
 #include "TESTMain.h"
 #include <wx/image.h>
 //*)
 
 IMPLEMENT_APP(TESTApp);
 
 bool TESTApp::OnInit()
 {
     //(*AppInitialize
     bool wxsOK = true;
     wxInitAllImageHandlers();
     if ( wxsOK )
     {
         TESTFrame* Frame = new TESTFrame(0);
         Frame->Show();
         SetTopWindow(Frame);
     }
     //*)
     return wxsOK;
 
 }```

5. wx_pch.h

 ```#ifndef WX_PCH_H_INCLUDED
 #define WX_PCH_H_INCLUDED
 
 // basic wxWidgets headers
 #include <wx/wxprec.h>
 
 #ifdef __BORLANDC__
     #pragma hdrstop
 #endif
 
 #ifndef WX_PRECOMP
     #include <wx/wx.h>
 #endif
 
 #ifdef WX_PRECOMP
     // put here all your rarely-changing header files
 #endif // WX_PRECOMP
 
 #endif // WX_PCH_H_INCLUDED```

6. TESTFram.wxs

 ```<?xml version="1.0" encoding="utf-8" ?>
 <wxsmith>
     <object class="wxFrame" name="TESTFrame">
         <size>914,512</size>
         <object class="wxBoxSizer" variable="BoxSizer1" member="no">
             <object class="sizeritem">
                 <object class="wxButton" name="ID_BUTTON1" variable="Button1" member="yes">
                     <label>Label</label>
                 </object>
                 <flag>wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL</flag>
                 <border>5</border>
                 <option>1</option>
             </object>
             <object class="sizeritem">
                 <object class="wxButton" name="ID_BUTTON2" variable="Button2" member="yes">
                     <label>Label</label>
                 </object>
                 <flag>wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL</flag>
                 <border>5</border>
                 <option>1</option>
             </object>
             <object class="sizeritem">
                 <object class="wxPanel" name="ID_PANEL1" variable="Panel1" member="yes">
                     <size>268,236</size>
                     <object class="wxBoxSizer" variable="BoxSizer2" member="no" />
                 </object>
                 <flag>wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL</flag>
                 <border>5</border>
                 <option>1</option>
             </object>
         </object>
         <object class="wxMenuBar" variable="MenuBar1" member="no">
             <object class="wxMenu" variable="Menu1" member="no">
                 <label>&amp;File</label>
                 <object class="wxMenuItem" name="idMenuQuit" variable="MenuItem1" member="no">
                     <label>Quit</label>
                     <accel>Alt-F4</accel>
                     <help>Quit the application</help>
                     <handler function="OnQuit" entry="EVT_MENU" />
                 </object>
             </object>
             <object class="wxMenu" variable="Menu2" member="no">
                 <label>Help</label>
                 <object class="wxMenuItem" name="idMenuAbout" variable="MenuItem2" member="no">
                     <label>About</label>
                     <accel>F1</accel>
                     <help>Show info about this application</help>
                     <handler function="OnAbout" entry="EVT_MENU" />
                 </object>
             </object>
         </object>
         <object class="wxStatusBar" name="ID_STATUSBAR1" variable="StatusBar1" member="yes">
             <fields>1</fields>
             <widths>-1</widths>
             <styles>wxSB_NORMAL</styles>
         </object>
     </object>
 </wxsmith>```


找到一些防止崩溃的程序。

  1. 不要将布局控件直接添加到框架中。先添加面板再添加sizer。
  2. 保持默认大小 属性 为包含布局控件的面板打开。 Final UI 控件可以有特定的高度和宽度。根据我的经验,如果我们有 UI 带有自定义 width/height 的控制器,我们无法为面板设置自己的宽度和高度,这些控制器被放置在类似 box sizer 的布局中。

在避免上述情况后,我能够运行程序没有崩溃。