问题描述
我正在使用CodeBlock IDE中的WxSmith学习gui编程。我按照以下步骤创建了一个示例项目
- 使用WxSmith 3.0版创建WxSmith项目
- 在项目的默认主框架中添加3列的box sizer
- 在每列中添加2个按钮和1个面板
- 将WxBoxSizer添加到面板中
- 构建项目并运行,构建成功,但生成运行时错误。
在使用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) () at /usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#1 0x00007ffff79b3d95 in wxSizerItem::InformFirstDirection(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和头文件。
- 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"),wxDefaultValidator,_T("ID_BUTTON1"));
BoxSizer1->Add(Button1,1,wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL,5);
Button2 = new wxButton(this,ID_BUTTON2,_T("ID_BUTTON2"));
BoxSizer1->Add(Button2,5);
Panel1 = new wxPanel(this,ID_PANEL1,wxSize(268,236),wxTAB_TRAVERSAL,_T("ID_PANEL1"));
BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
Panel1->SetSizer(BoxSizer2);
SetSizer(BoxSizer2);
Layout();
BoxSizer1->Add(Panel1,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,_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,(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>&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>```
解决方法
找到了一些防止崩溃的程序。
- 请勿将布局控件直接添加到框架。首先添加一个面板,然后在该面板中添加sizer。
- 将包含布局控件的面板的默认尺寸属性保持为ON。最终的UI控件可以具有特定的高度和宽度。根据我的经验,如果我们将具有自定义宽度/高度的UI控制器放置在类似于box sizer的布局中,则无法为面板设置自己的宽度和高度。
在避免上述情况后,我能够在不崩溃的情况下运行该程序。