If you want to build a good desktop application in C++, then you need to have a good GUI as well. I have tried many GUI libraries and out of which I think wxWidgets is easiest one to use.
This edition of techblog introduces GUI programming in C++ using wxWidgets. Please note that you need to install wxWidgets library in your system, if you want to try this tutorial. You can download it from this website.
Let’s begin this tutorial by simply using some of the functions provided by wxWidget. This will allow us to check whether the library has been installed properly. You can use an IDE for doing these tutorials. I recommend Anjuta or CodeBlocks for C/C++ development.
Testing wxWidgets library installation
After installing wxWidgets in your system you can test it by writing a very simple console application –
#include <wx/string.h>
int main(int argc, char **argv)
{
wxPuts(wxT("A wxWidgets console application"));
}
Now compile the code and run it. You should see an output similar to the one shown below (Please scroll down to see the explanation of the functions used here)
First GUI application
Let’s call our first application as ‘TechApp’. The basic thing that you need for any GUI application using wxWidget is wxFrame – which is the core frame whose dimensions can be specified using parameters. Let’s create a class ‘TechblogSimple’ by extending the wxFrame class (which comes from the wx.h file). It takes a string and sets that as the title.
Here is the code:
#include <wx/wx.h>
class TechblogSimple : public wxFrame
{
public:
TechblogSimple (const wxString& title);
};
Save this code as a header file – say techblogsimple.h
Though we have extended the class in the header, we have not defined the size and initial position of the frame. We can do that by using:
#include "techblogsimple.h"
TechblogSimple ::TechblogSimple(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 250))
{
Centre();
}
Save this file as simple.cpp. Here the function ‘Centre()’ centres the window (you may note that you can pass horizontal and vertical parameters to this function) and wxString() is used to handle Unicode strings of arbitrary length.
Now let us extend the wxApp (which is the application itself) and call the OnInit() function – which will create the main window of the application.
Here is the code:
#include <wx/wx.h>
class TechApp : public wxApp
{
public:
virtual bool OnInit();
};
Save the code as ‘main.h’
Now pass the string that you want to display as the title (using wxT() – which is a macro for passing strings)
#include <wx/string.h>
int main(int argc, char **argv)
{
wxPuts(wxT("A sample console application : Techblog"));
}
Save this as ‘main.cpp’.
Now we are done with the coding. Let’s test our application by compiling and running it. (If you are using an IDE you can use the build and run command straight from IDE itself)
You can issue the following command for compiling the code:
g++ main.cpp main.h simpletechblog.cpp simpletechblog.h `wx-config –cxxflags –libs` -o TechApp
Now call the object TechApp from the terminal:
We have created our first GUI application using wxWidgets. In the next edition, I will show you how to add a menu bar and add items to it!




Join Techblog
Facebook Group
Read
Digg entries
Add techblog to
Google reader