Hello World

From WxWiki
Jump to: navigation, search

The first example is the well-known Hello World application. The application will show a window displaying 'Hello World' in its statusbar.

HelloWorldApp.h - The HelloWorldApp definition

Each wxWindow application needs an object derived from wxApp. Each application overrides the OnInit() method for initializing the application. You can, for example, create your main window here.

// The HelloWorldApp class. This class shows a window
// containing a statusbar with the text "Hello World"
class HelloWorldApp : public wxApp
	virtual bool OnInit();

HelloWorldApp.cpp - The implementation of HelloWorldApp

For the main window, you use the wxFrame class. This class provides a window whose size and position can be changed by the user. It has thick borders and a title bar. In addition, you can provide it a menu bar, a statusbar and a toolbar. Example 1.5 shows the implementation of HelloWorldApp.

// For compilers that don't support precompilation, include "wx/wx.h"
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#	include "wx/wx.h"
#include "HelloWorldApp.h"
// This is executed upon startup, like 'main()' in non-wxWidgets programs.
bool HelloWorldApp::OnInit()
	wxFrame *frame = new wxFrame((wxFrame*) NULL, -1, _T("Hello wxWidgets World"));
	frame->SetStatusText(_T("Hello World"));
	return true;

When your compiler supports precompiled headers, you can use the wxprec header file. When it doesn't, you should include wx.h, which includes all necessary header files for wxWindow. You can also include each header file separately for each control.

The macros DECLARE_APP and IMPLEMENT_APP do the following for us:

  • When the platform needs one, it creates a main() or WinMain() method.
  • It creates the global method wxGetApp(). You can use this function to retrieve a reference to the one and only application object:
HelloWorldApp &app = ::wxGetApp();

You could be wondering why the frame variable isn't deleted anywhere. By setting the frame as the top window of the application, the application will delete the frame for us (for a more in-depth explanation, see Avoiding Memory Leaks).

Some broken compilers don't allow NULL to be casted to wxFrame* implicitly, so that's why we do it explicitly, just to be on the safe side.

  • Really? Is this true even now? The C++ standard (which is 5 years old now) requires that NULL, which is 0 (or 0L, or 0s -- but not (void *)0) can be cast to any pointer type. Even MSVC 6 gets this right -- anyone using such a broken compiler should probably upgrade.

After the frame is constructed, a statusbar is created with the CreateStatusBar method. The text of the statusbar is set to "Hello World". Calling Show() shows the frame. Show() is a method of the wxWindow class which wxFrame derives from.

When OnInit() returns false, the application immediately stops. This way you can stop the application when something went wrong during the initialization phase.

Thanks to Franky Braem for the initial content for this page


To compile under linux using g++, use the following command:

 g++ HelloWorldApp.cpp `wx-config --libs` `wx-config --cxxflags` -o HelloWorldApp