Wt examples 4.7.2
hello.C
Go to the documentation of this file.
1/*
2 * Copyright (C) 2008 Emweb bv, Herent, Belgium.
3 *
4 * See the LICENSE file for terms of use.
5 */
6
7#include <Wt/WApplication.h>
8#include <Wt/WBreak.h>
9#include <Wt/WContainerWidget.h>
10#include <Wt/WLineEdit.h>
11#include <Wt/WPushButton.h>
12#include <Wt/WText.h>
13
14/*
15 * A simple hello world application class which demonstrates how to react
16 * to events, read input, and give feed-back.
17 */
18class HelloApplication : public Wt::WApplication
19{
20public:
21 HelloApplication(const Wt::WEnvironment& env);
22
23private:
24 Wt::WLineEdit *nameEdit_;
25 Wt::WText *greeting_;
26
27 void greet();
28};
29
30/*
31 * The env argument contains information about the new session, and
32 * the initial request. It must be passed to the WApplication
33 * constructor so it is typically also an argument for your custom
34 * application constructor.
35*/
36HelloApplication::HelloApplication(const Wt::WEnvironment& env)
37 : WApplication(env)
38{
39 setTitle("Hello world"); // application title
40
41 root()->addWidget(std::make_unique<Wt::WText>("Your name, please ? ")); // show some text
42
43 nameEdit_ = root()->addWidget(std::make_unique<Wt::WLineEdit>()); // allow text input
44 nameEdit_->setFocus(); // give focus
45
46 auto button = root()->addWidget(std::make_unique<Wt::WPushButton>("Greet me."));
47 // create a button
48 button->setMargin(5, Wt::Side::Left); // add 5 pixels margin
49
50 root()->addWidget(std::make_unique<Wt::WBreak>()); // insert a line break
51 greeting_ = root()->addWidget(std::make_unique<Wt::WText>()); // empty text
52
53 /*
54 * Connect signals with slots
55 *
56 * - simple Wt-way: specify object and method
57 */
58 button->clicked().connect(this, &HelloApplication::greet);
59
60 /*
61 * - using an arbitrary function object, e.g. useful to bind
62 * values with std::bind() to the resulting method call
63 */
64 nameEdit_->enterPressed().connect(std::bind(&HelloApplication::greet, this));
65
66 /*
67 * - using a lambda:
68 */
69 button->clicked().connect([=]() {
70 std::cerr << "Hello there, " << nameEdit_->text() << std::endl;
71 });
72}
73
75{
76 /*
77 * Update the text, using text input into the nameEdit_ field.
78 */
79 greeting_->setText("Hello there, " + nameEdit_->text());
80}
81
82int main(int argc, char **argv)
83{
84 /*
85 * Your main method may set up some shared resources, but should then
86 * start the server application (FastCGI or httpd) that starts listening
87 * for requests, and handles all of the application life cycles.
88 *
89 * The last argument to WRun specifies the function that will instantiate
90 * new application objects. That function is executed when a new user surfs
91 * to the Wt application, and after the library has negotiated browser
92 * support. The function should return a newly instantiated application
93 * object.
94 */
95 return Wt::WRun(argc, argv, [](const Wt::WEnvironment &env) {
96 /*
97 * You could read information from the environment to decide whether
98 * the user has permission to start a new application
99 */
100 return std::make_unique<HelloApplication>(env);
101 });
102}
HelloApplication(const Wt::WEnvironment &env)
Definition: hello.C:36
Wt::WText * greeting_
Definition: hello.C:25
void greet()
Definition: hello.C:74
Wt::WLineEdit * nameEdit_
Definition: hello.C:24
int main(int argc, char **argv)
Definition: hello.C:82

Generated on Fri May 13 2022 for the C++ Web Toolkit (Wt) by doxygen 1.9.4