This mini tutorial aims to help people make quick judgements concerning syntax and basic principles. The complete Hello World program is here
You have to include wxWidgets's header files, of course. This can be done on a file by file basis (such as #include "wx/window.h") or using one global include (#include "wx/wx.h"). This is also useful when using precompiled headers.
#include "wx/wx.h"
Practically every app should define a new class derived from wxApp. By overriding wxApp's OnInit() the program can be initialized, e.g. by creating a new main window.
class MyApp: public wxApp
{
virtual bool OnInit();
};
The main window is created by deriving a class from wxFrame and giving it a menu and a status bar in its constructor. Also, any class that wishes to respond to any "event" (such as mouse clicks or messages from the menu or a button) must declare an event table using the macro below. Finally, the way to react to such events must be done in "handlers". In our sample, we react to two menu items, one for "Quit" and one for displaying an "About" window. These handlers should not be virtual.
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
In order to be able to react to a menu command, it must be given a unique identifier such as a const or an enum.
enum
{
ID_Quit = 1,
ID_About,
};
We then procede to actually implement an event table in which the events are routed to their respective handler functions in the class MyFrame. There are predefined macros for routing all common events, ranging from the selection of a list box entry to a resize event when a user resizes a window on the screen.
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit)
EVT_MENU(ID_About, MyFrame::OnAbout)
END_EVENT_TABLE()
As in all programs there must be a "main" function. Under wxWidgets main is implemented using this macro, which creates an application instance and starts the program.
IMPLEMENT_APP(MyApp)
As mentionend above, wxApp::OnInit() is called upon startup and should be used to initialize the program, maybe showing a "splash screen" and creating the main window (or several). The frame should get a title bar text ("Hello World") and a position and start-up size. One frame can also be declared to be the top window. Returning TRUE indicates a successful intialization.
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( _("Hello World"), wxPoint(50, 50), wxSize(450, 340) );
frame->Show(true);
SetTopWindow(frame);
return true;
}
In the constructor of the main window (or later on) we create a menu with two menu items as well as a status bar to be shown at the bottom of the main window. Both have to be "announced" to the frame with respective calls.
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, -1, title, pos, size)
{
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, _("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _("E&xit") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, _("&File") );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( _("Welcome to wxWidgets!") );
}
Here are the actual event handlers. MyFrame::OnQuit() closes the main window by calling Close(). The paramter TRUE indicates that other windows have no veto power such as after asking "Do you really want to close?". If there is no other main window left, the applicatin will quit.
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}
MyFrame::OnAbout() will display a small window with some text in it. In this case a typical "About" window with information about the program.
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox( _("This is a wxWidgets Hello world sample"),
_("About Hello World"),
wxOK | wxICON_INFORMATION, this );
}
Under Unix (including Linux and OS X and nearly any system that uses GCC), you can use the following statement to compile the program:
g++ hworld.cpp `wx-config --libs` `wx-config --cxxflags` -o hworld
Finally, you might want to start the program with:
./hworld
A version of hello world using Connect() instead of event tables is here
引出出处:http://www.wxwidgets.org/docs/tutorials/hello.htm
分享到:
相关推荐
在`wxWidget-HelloWorld-master`这个项目中,我们通常会看到以下几个关键文件: 1. **main.cpp**: 这是程序的入口点,通常包含了初始化`wxApp`对象(代表整个应用程序),创建主窗口`wxFrame`以及定义事件处理函数...
在本实例中,"wxWidgets的HelloWorld实例"是一个基础的入门项目,用来验证wxWidgets库和开发环境的正确配置。 **HelloWorld程序** “Hello, World!”程序是编程初学者的第一个传统程序,它简单地在屏幕上打印出...
这段代码创建了一个简单的窗口,并在其中显示 "Hello World!" 文本。 **2. Qt Creator** Qt 提供了一个名为 Qt Creator 的集成开发环境 (IDE),它集成了代码编辑器、调试器、构建工具等功能,大大简化了 Qt 应用...
**:通过简单的“Hello, World!”程序介绍Qt的基本用法,包括如何创建窗口、显示文本等基本操作。 - **信号槽机制简介**:信号槽是Qt的核心概念之一,用于实现对象间的通信。本节将介绍信号和槽的概念、连接方法以及...
打开Eclipse,创建一个新的C++项目,选择Hello World C++ Project,Toolchains为minGW GCC。然后,在项目的Properties中,选择C/C++ Build->Settings,在Tool Settings中进行相关配置。首先,选择GCC C++ Compiler->...
**:编写第一个简单的 wxWidgets 应用,通常是一个显示 "Hello, World!" 的窗口。 4. **控件和布局**:学习如何创建和使用各种控件,如按钮、文本框等,并掌握布局管理器的使用,以便自适应不同屏幕尺寸。 5. **事件...
书中还提供了多个实例来辅助理解,包括一个简单的“Hello, World!”程序和更完整的例子,用以展示如何组织代码结构、如何编译和运行程序、以及应用程序一般执行过程。 由于本书是关于wxWidgets的中文教程,原作者和...
QLabel *label = new QLabel("Hello, world!"); label->show(); return app.exec(); } ``` 这个程序从第4行的main函数开始,创建了一个QApplication对象,管理应用程序的全局资源。接着,创建了一个QLabel对象...
在这个例子中,我们首先包含了`QApplication`和`QLabel`这两个类的头文件,然后在`main`函数中创建了一个`QApplication`对象,接着创建了一个`QLabel`对象,并设置其文本为"Hello World!",最后调用`show()`方法使...
从Hello World这样的基础示例开始,逐步深入学习Qt的各个模块,你将能掌握创建跨平台应用的强大能力。 综上所述,Qt不仅是一个GUI库,更是C++开发者的全能伙伴,它在提升开发效率、简化复杂性方面发挥了关键作用。...
从编写第一个“Hello, World!”程序开始,逐步深入Qt的GUI编程世界,开发者将能够构建出功能丰富、界面友好的应用程序。 #### 结语 Qt的出现不仅丰富了C++的生态系统,更为跨平台应用的开发提供了强大工具。无论是...
首先,文档提到了“HELLO,WORLD!”,这通常是学习任何编程语言或框架的第一个入门程序,目的是帮助初学者搭建最基本的编程环境,并运行一个简单的示例程序。通过这个步骤,学习者可以初步了解QT框架的使用和操作流程...