Project

General

Profile

Different version of Hello world

Added by Giovanni Marchetto about 5 years ago

Hi All,

i'm a newbie with WT, and i've started with the classic hello world.

From documentation i've see different version of hello world :

This one form "A hands-on introduction to Wt"

@

#include <Wt/WApplication.h>

#include <Wt/WBreak.h>

#include <Wt/WContainerWidget.h>

#include <Wt/WLineEdit.h>

#include <Wt/WPushButton.h>

#include <Wt/WText.h>

class HelloApplication : public Wt::WApplication

{

public:

HelloApplication(const Wt::WEnvironment& env);

private:

Wt::WLineEdit *nameEdit_;

Wt::WText *greeting_;

};

HelloApplication::HelloApplication(const Wt::WEnvironment& env)

: Wt::WApplication(env)

{

setTitle("Hello world");

root()->addWidget(std::make_uniqueWt::WText("Your name, please? "));

nameEdit_ = root()->addWidget(std::make_uniqueWt::WLineEdit());

Wt::WPushButton *button = root()->addWidget(std::make_uniqueWt::WPushButton("Greet me."));

root()->addWidget(std::make_uniqueWt::WBreak());

greeting_ = root()->addWidget(std::make_uniqueWt::WText());

auto greet = [this]{

greeting_setText("Hello there, " + nameEdit_>text());

};

button->clicked().connect(greet);

}

int main(int argc, char **argv)

{

return Wt::WRun(argc, argv, [](const Wt::WEnvironment& env) {

return std::make_unique(env);

});

}

@

This one from GitHub :

@

/*

  • Copyright © 2008 Emweb bvba, Heverlee, Belgium.
  • See the LICENSE file for terms of use.
    */

#include <Wt/WApplication.h>

#include <Wt/WBreak.h>

#include <Wt/WContainerWidget.h>

#include <Wt/WLineEdit.h>

#include <Wt/WPushButton.h>

#include <Wt/WText.h>

/*

  • A simple hello world application class which demonstrates how to react
  • to events, read input, and give feed-back.
    */
    class HelloApplication : public Wt::WApplication
    {
    public:
    HelloApplication(const Wt::WEnvironment& env);

private:

Wt::WLineEdit *nameEdit_;

Wt::WText *greeting_;

void greet();

};

/*

  • The env argument contains information about the new session, and
  • the initial request. It must be passed to the WApplication
  • constructor so it is typically also an argument for your custom
  • application constructor.
    */
    HelloApplication::HelloApplication(const Wt::WEnvironment& env)
    : WApplication(env)
    {
    setTitle("Hello world"); // application title

root()->addWidget(Wt::cpp14::make_uniqueWt::WText("Your name, please ? ")); // show some text

nameEdit_ = root()->addWidget(Wt::cpp14::make_uniqueWt::WLineEdit()); // allow text input

nameEdit_->setFocus(); // give focus

auto button = root()->addWidget(Wt::cpp14::make_uniqueWt::WPushButton("Greet me."));

// create a button

button->setMargin(5, Wt::Side::Left); // add 5 pixels margin

root()->addWidget(Wt::cpp14::make_uniqueWt::WBreak()); // insert a line break

greeting_ = root()->addWidget(Wt::cpp14::make_uniqueWt::WText()); // empty text

/*

  • Connect signals with slots
  • - simple Wt-way: specify object and method
    */
    button->clicked().connect(this, &HelloApplication::greet);

/*

  • - using an arbitrary function object, e.g. useful to bind
  • values with std::bind() to the resulting method call
    */
    nameEdit_->enterPressed().connect(std::bind(&HelloApplication::greet, this));

/*

  • - using a lambda:
    */
    button->clicked().connect([=]() {
    std::cerr << "Hello there, " << nameEdit_->text() << std::endl;
    });
    }

void HelloApplication::greet()

{

/*

  • Update the text, using text input into the nameEdit_ field.
    */
    greeting_setText("Hello there, " + nameEdit_>text());
    }

int main(int argc, char **argv)

{

/*

  • Your main method may set up some shared resources, but should then
  • start the server application (FastCGI or httpd) that starts listening
  • for requests, and handles all of the application life cycles.
  • The last argument to WRun specifies the function that will instantiate
  • new application objects. That function is executed when a new user surfs
  • to the Wt application, and after the library has negotiated browser
  • support. The function should return a newly instantiated application
  • object.

    */

    return Wt::WRun(argc, argv, [](const Wt::WEnvironment &env) {

    /*

  • You could read information from the environment to decide whether

  • the user has permission to start a new application

    */

    return Wt::cpp14::make_unique(env);

    });

    }

@

The first one for me it's more clear, but which way is preferred?

Thanks a lot.


Replies (3)

RE: Different version of Hello world - Added by lm at about 5 years ago

std::make_unique is preferable. I think the one that uses Wt::cpp14::make_unique was written without std::make_unique available.

RE: Different version of Hello world - Added by Roel Standaert about 5 years ago

The one in the examples directory has been extended a bit to demonstrate more different ways to connect slots. The tutorial shows the basic hello world example.

As for the Wt::cpp14::make_unique thing: that's really only for when you are compiling in C+11 mode and you don't have C+14's std::make_unique. There's no reason to use it if you use a modern compiler.

RE: Different version of Hello world - Added by Giovanni Marchetto about 5 years ago

Hi, Thanks a lot for reply. Now it's more clear.

    (1-3/3)