Example C++ GUI application using Wxwidgets

Okay from the previous post (installation wxwidgets), now I want share how to write example gui application using wxwidgets, and from this post, I use CodeBlocks for the development editor.

Installation CodeBlocks

  1. For windows user, you can download codeblocks from here http://www.codeblocks.org/downloads and choose download the binary release. Then install it.
  2. For Linux user (example ubuntu), open your terminal and run this command:
    $ sudo add-apt-repository ppa:pasgui/ppa
    $ sudo apt-get update
    $ sudo apt-get install codeblocks
    

Sample Applications:

Okay now we will create sample application using c++ and wxwidget.

  1. Open your codeblocks and click File → New → Project, then select empty project
  2. Then write project name is “Test”, and for Folder to create project in:, you can save project in the folder that you want
  3. Then click next until finish. If right you can see new project like this:
  4. Then click Test project and click File → New → File, then select C/C++ Header, then click “Go”
  5. If show app prompt like this in “Filename with full path:”, just click the browse button and write test.h, and don’t forget to checklist all “in build target (s)” and you can get result like this (if using Linux)
    And if you using windows (example I save test.h to D:\workspaces\c++\Test directory):

    and click finish
  6. Now you can get new directory in Test project, the directory named “Headers”:
  7. Do same for create file test.cpp, main.h, and main.cpp and look like this:

The Source:

For the source, first open your test.h and write like this:

#ifndef TEST_H
#define TEST_H

#include <wx/wx.h>

class Test : public wxFrame
{
public:
    Test(const wxString& title);

    // Destructor
    ~Test();
};

#endif // TEST_H

Source for test.cpp:

#include "test.h"

Test::Test(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150))
{
    Centre();
}

Test::~Test() {}

Source for main.h:

#ifndef MAIN_H
#define MAIN_H

#include <wx/wx.h>
class MainApp : public wxApp
{
public:
    virtual bool OnInit();
};

DECLARE_APP(MainApp)

#endif // MAIN_H

Source for main.cpp:

#include "main.h"
#include "test.h"

IMPLEMENT_APP(MainApp)

bool MainApp::OnInit()
{
    Test *test = new Test(wxT("Test"));
    test->Show(true);

    return true;
}

Compile Sample Program

For Linux user

Now right click Test project and select build like this:

Then if you get an error like this:

||=== Build: Debug in Test1 (compiler: GNU GCC Compiler) ===|
D:\workspaces\c++\Test\main.h|4|fatal error: wx/wx.h: No such file or directory|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Follow this instruction:

  1. Right click the project and click properties:
  2. In tab menu Project settings, click Project’s build options:
  3. Then click “Other options” tab and add this code:
    `wx-config --cflags`
    

  4. Then click tab menu “Linker settings” and in other linker options, write this:
    `wx-config --libs`
    

  5. Now right click Test project and select rebuild, make sure you not have error:
  6. And the last, click the Test project and click Build → run or CTRL + F10. If right and no error, the program will show like this:

For Windows User:

Now right click Test project and select build like this:

Then if you get an error like this:

||=== Build: Debug in Test1 (compiler: GNU GCC Compiler) ===|
D:\workspaces\c++\Test\main.h|4|fatal error: wx/wx.h: No such file or directory|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Follow this instruction:

  1. Right click the project and click properties:
  2. In tab menu Project settings, click Project’s build options:
  3. Then click #defines tab and add this code:
    __GNUWIN32__
    __WXMSW__
    WXUSINGDLL
    

  4. Now open menu tab “Linker settings” and in “Link libraries” click button add → and click button browse and browse to C:\wx\lib\gcc_dll and select this file libwxbase30u.a, libwxjpeg.a, libwxmsw30u_core.a, libwxpng.a, libwxzlib.a:
  5. Then open menu tab “Search directories” and in menu compiler click button add and click browse button and select C:\wx\include directory. Do same to menu “Resource compiler”:
  6. Same at “Search directories”, now open tab menu “Linker” and click button add and click browse and select C:\wx\lib\gcc_dll:
  7. Now right click Test project and select rebuild, make sure you not have error
  8. And the last, click the Test project and click Build → run or CTRL + F10. If right and no error, the program will show like this:

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s