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

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13