avatar

Andres Jaimes

Two ways to create a Hello World application with Wt (Witty)

By Andres Jaimes

Let’s start by talking a little bit about some elements and methods you will find in the code.

Inheriting from WApplication or WContainerWidget are the most common ways to start an application using Wt. A WApplication instance will be in charge of creating your html, head and body tags. A WContainerWidget is usually a div tag (it can also be turned into a ul or ol by using its setList method). If you want to add items to your body tag, you have to ask your application for the root element by using the root() method – the root element does not really correspond to the body tag but it is close to it… it is a div.

So let’s begin coding. Here you have, two ways to implement a basic application:

 

By inheriting WApplication

For our first example we will use WApplication as our parent class.

Create a file named main.cpp and copy the following code into it:

#include <Wt/WApplication>
#include <Wt/WContainerwidget>
#include <Wt/WText>

using namespace Wt;

class ControlExample: public WApplication {
public:
    ControlExample(const WEnvironment &env): WApplication(env) {
        setTitle("Hello World Example");
        WContainerWidget* wc = new WContainerWidget(root());
        wc->addWidget(new WText("Hello World!"));
    }
};

WApplication* createApplication(const WEnvironment &env) {
    return new ControlExample(env);
}

int main(int argc, char** argv) {
    return WRun(argc, argv, &createApplication);
}

 

 

By inheriting WContainerWidget

For our second example we will use WContainerWidget as our parent class.

Create a file named main.cpp and copy the following code into it:

#include <Wt/WApplication>
#include <Wt/WContainerwidget>
#include <Wt/WText>

using namespace Wt;

class ControlExample: public WContainerWidget {
public:
    ControlExample(WContainerWidget *parent = 0): WContainerWidget(parent) {
        addWidget(new WText("Hello World!"));
    }
};

WApplication* createApplication(const WEnvironment &env) {
    WApplication* app = new WApplication(env);
    app->setTitle("Control Test");
    app->root()->addWidget(new ControlExample());

    return app;
}

int main(int argc, char** argv) {
    return WRun(argc, argv, &createApplication);
}

 

In order to successfully compile any of these applications you have to let your compiler know about the additional include and lib folders and files.

  • Add your Wt _include_folder. You can do it by using the –I parameter for command line compilers, or if you are using Visual Studio by right clicking on your project, selecting Properties and going to C/C++ > General > Additional Include Directories
  • Add your Wt and your boost library folders. You can do it by using the –L parameter for command line compilers, or if you are using Visual Studio by right clicking on your project, selecting Properties and going to Linker > General > Additional Library Directories. Remember boostis required by Wt to work.
  • Make a reference to your wtd.lib and wthttpd.lib library files. These files are found in your Wt folder and they are the debug libraries for Wt. If you are using Visual Studio right click on your project, select Properties, go to Linker > Input > Additional Dependencies.

 

To run your application you have to add the following parameters to your command line:

--http-address=0.0.0.0 --http-port=8080 --deploy-path=/hello --docroot=.

 

If you are using Visual Studio you can add them by right clicking on your project, then going to Properties > Configuration Properties > Debugging > Command Arguments.

Open a web browser and go to http://localhost:8080/hello to test your application.

Happy coding…