簡體   English   中英

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

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

我正在 CodeBlock IDE 中使用 WxSmith 學習 gui 編程。 我用以下步驟創建了一個示例項目

  1. Crete 使用 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. 對於包含布局控件的面板,保持默認大小屬性為 ON。 最終 UI 控件可以具有特定的高度和寬度。 根據我的經驗,如果我們有自定義寬度/高度的 UI 控制器放置在像 box sizer 這樣的布局中,我們就不能為面板設置我們自己的寬度和高度。

避免上述情況后,我能夠運行程序而不會崩潰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM