The
"Hello, World!" program in Wt: • include • include • include • include • include • include /* * 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()->addNew("Your name, please ? "); // show some text nameEdit_ = root()->addNew(); // allow text input nameEdit_->setFocus(); // give focus auto button = root()->addNew("Greet me."); // create a button button->setMargin(5, Wt::Side::Left); // add 5 pixels margin root()->addNew(); // insert a line break greeting_ = root()->addNew(); // 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 text() 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 std::make_unique(env); }); } ==See also==