Wt examples  4.0.1
hello.C
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Emweb bvba, Heverlee, 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  */
19 {
20 public:
22 
23 private:
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 */
37  : WApplication(env)
38 {
39  setTitle("Hello world"); // application title
40 
41  root()->addWidget(Wt::cpp14::make_unique<Wt::WText>("Your name, please ? ")); // show some text
42 
43  nameEdit_ = root()->addWidget(Wt::cpp14::make_unique<Wt::WLineEdit>()); // allow text input
44  nameEdit_->setFocus(); // give focus
45 
46  auto button = root()->addWidget(Wt::cpp14::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(Wt::cpp14::make_unique<Wt::WBreak>()); // insert a line break
51  greeting_ = root()->addWidget(Wt::cpp14::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  */
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 
82 int 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 Wt::cpp14::make_unique<HelloApplication>(env);
101  });
102 }
const WString & text() const
Wt::WLineEdit * nameEdit_
Definition: hello.C:24
void setTitle(const WString &title)
bool setText(const WString &text)
Wt::Signals::connection connect(const F &function)
Wt::WText * greeting_
Definition: hello.C:25
void greet()
Definition: hello.C:74
int main(int argc, char **argv)
Definition: hello.C:82
virtual void setFocus(bool focus) override
EventSignal & enterPressed()
WContainerWidget * root() const
HelloApplication(const Wt::WEnvironment &env)
Definition: hello.C:36
virtual void addWidget(std::unique_ptr< WWidget > widget)
WApplication(const WEnvironment &environment)

Generated on Thu Oct 19 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11