上一节中我们讲解了如何在window和mac下配置一个wxWidgets项目,这一节我们可以开发一个简单的跨平台应用了。
界面设计软件wxFormBuilder
用C++进行桌面应用开发,最痛苦的当然是界面设计了,特别是界面布局,事件相应等等,然而用wxWidgets进行桌面应用开发,我们有一个界面设计神器wxFormBuilder!
wxFormBuilder不仅可以交互式的进行界面设计,而且可以帮助我们生成事件响应代码,更厉害的是,它能同时生成python和C++两种语言的代码,所以对于性能要求不高的应用,一般我都是用wxPython进行开发的,对于性能要求比较高的应用,你完全可以切换到用C++进行开发,然而整个界面设计的代码基本上都是通用的,简直太棒了!
下载wxFormBuilder
到wxFormBuilder的github主页下载即可。我的mac是M1的处理器,通过UTM安装的window11是ARM 64的版本,发现下载X64版本的不能运行,x86版本是可以运行的,所以安装了x86的版本。mac的直接下载dmg文件安装即可。 安装后的wxFormBuilder主界面如下。
界面开发示例
下面这个例子,我们用wxFormBuilder建了一个MainFrame类和AboutDialog类,并实现点击关于菜单时,显示关于对话框,非常简单。其他的界面元素大家自己参考wxWidgets的官方文档即可,这里不会一个个的去详细讲解。
MainFrame.h的源代码:
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version 3.10.1-0-g8feb16b3)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////
#pragma once
#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/statusbr.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/string.h>
#include <wx/notebook.h>
#include <wx/sizer.h>
#include <wx/bitmap.h>
#include <wx/image.h>
#include <wx/icon.h>
#include <wx/menu.h>
#include <wx/frame.h>
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/// Class MainFrame
///////////////////////////////////////////////////////////////////////////////
class MainFrame : public wxFrame
{
private:
protected:
wxMenuBar* m_menubarMain;
wxMenu* m_menuHelp;
// Virtual event handlers, override them in your derived class
virtual void m_menuItemAboutOnMenuSelection( wxCommandEvent& event );
public:
MainFrame();
MainFrame( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 1259,923 ), long style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
~MainFrame();
};
MainFrame.cpp的源代码:
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version 3.10.1-0-g8feb16b3)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////
#include "MainFrame.h"
#include "AboutDialog.h"
#include "StrategyPanel.h"
///////////////////////////////////////////////////////////////////////////
MainFrame::MainFrame() : wxFrame(nullptr, wxID_ANY, wxString(wxT("K量化")))
{
this->SetPosition(wxDefaultPosition);
this->SetSize(wxSize(1024, 768));
this->SetWindowStyle(wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL);
this->SetSizeHints(wxDefaultSize, wxDefaultSize);
wxBoxSizer *bSizerMain;
bSizerMain = new wxBoxSizer(wxVERTICAL);
this->SetSizer(bSizerMain);
this->Layout();
m_menubarMain = new wxMenuBar(0);
m_menuFile = new wxMenu();
m_menubarMain = new wxMenuBar(0);
m_menuHelp = new wxMenu();
wxMenuItem *m_menuItemAbout;
m_menuItemAbout = new wxMenuItem(m_menuHelp, wxID_ANY, wxString(wxT("关于...")), wxEmptyString, wxITEM_NORMAL);
m_menuHelp->Append(m_menuItemAbout);
m_menubarMain->Append(m_menuHelp, wxT("帮助"));
this->SetMenuBar(m_menubarMain);
this->Centre(wxBOTH);
// Connect Events
m_menuHelp->Bind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::m_menuItemAboutOnMenuSelection), this, m_menuItemAbout->GetId());
}
MainFrame::~MainFrame()
{
// Disconnect Events
}
void MainFrame::m_menuItemAboutOnMenuSelection(wxCommandEvent &event)
{
AboutDialog aboutDialog(this,wxID_ABOUT,wxString(wxT("关于...")),wxDefaultPosition,wxSize(215,129),wxDEFAULT_FRAME_STYLE);
aboutDialog.ShowModal();
event.Skip();
}
AboutDialog.h的源代码:
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version 3.10.1-0-g8feb16b3)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////
#pragma once
#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/string.h>
#include <wx/stattext.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/sizer.h>
#include <wx/dialog.h>
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/// Class AboutDialog
///////////////////////////////////////////////////////////////////////////////
class AboutDialog : public wxDialog
{
private:
protected:
wxStaticText* m_staticTextAuthor;
wxStaticText* m_staticTextVersion;
wxStaticText* m_staticTextQQ;
public:
AboutDialog( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 389,160 ), long style = wxDEFAULT_DIALOG_STYLE );
~AboutDialog();
};
AboutDialog.cpp的源代码:
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version 3.10.1-0-g8feb16b3)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////
#include "AboutDialog.h"
///////////////////////////////////////////////////////////////////////////
AboutDialog::AboutDialog(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &pos, const wxSize &size, long style) : wxDialog(parent, id, title, pos, size, style)
{
this->SetSizeHints(wxDefaultSize, wxDefaultSize);
wxBoxSizer *bSizerAbout;
bSizerAbout = new wxBoxSizer(wxVERTICAL);
m_staticTextAuthor = new wxStaticText(this, wxID_ANY, wxT("作者:kevinmeng"), wxDefaultPosition, wxDefaultSize, 0);
m_staticTextAuthor->Wrap(-1);
bSizerAbout->Add(m_staticTextAuthor, 0, wxALL, 5);
m_staticTextVersion = new wxStaticText(this, wxID_ANY, wxT("版本:V1.0.1"), wxDefaultPosition, wxDefaultSize, 0);
m_staticTextVersion->Wrap(-1);
bSizerAbout->Add(m_staticTextVersion, 0, wxALL, 5);
m_staticTextQQ = new wxStaticText(this, wxID_ANY, wxT("联系QQ:xxx"), wxDefaultPosition, wxDefaultSize, 0);
m_staticTextQQ->Wrap(-1);
bSizerAbout->Add(m_staticTextQQ, 0, wxALL, 5);
this->SetSizer(bSizerAbout);
this->Layout();
this->Centre(wxBOTH);
}
AboutDialog::~AboutDialog()
{
}
main.cpp的源代码:
// wxWidgets "Hello World" Program available on https://docs.wxwidgets.org/trunk/overview_helloworld.html
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include "MainFrame.h"
class StrategyApp : public wxApp
{
public:
virtual bool OnInit();
};
wxIMPLEMENT_APP(StrategyApp);
bool StrategyApp::OnInit()
{
MainFrame *frame = new MainFrame();
frame->Show(true);
return true;
}
运行效果:
这样,一个简单的wxWidgets应用就搭建好了,基本上界面的事情可以交给wxFormBuilder,我们只要关心业务代码即可。对于界面要求不是很高的小应用,wxWidgets完全可以应付,根本不需要QT,更别提臃肿且不能跨平台的MFC了!
评论区