Wt examples 3.3.12
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>
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
17using 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{
25public:
27
28private:
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
96int 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
WText * greeting_
Definition hello.C:30
WLineEdit * nameEdit_
Definition hello.C:29
void greet()
Definition hello.C:79
HelloApplication(const WEnvironment &env)
Definition hello.C:41
Wt::Signals::connection connect(const F &function)
friend friend class WLineEdit
WEnvironment & env()
WContainerWidget * root() const
void setTitle(const WString &title)
virtual void addWidget(WWidget *widget)
EventSignal & enterPressed()
EventSignal< WMouseEvent > & clicked()
const WString & text() const
bool setText(const WString &text)
virtual void setFocus(bool focus)
virtual void setMargin(const WLength &margin, WFlags< Side > sides=All)
WApplication * createApplication(const WEnvironment &env)
Definition hello.C:87
int main(int argc, char **argv)
Definition hello.C:96

Generated on Fri May 17 2024 for the C++ Web Toolkit (Wt) by doxygen 1.9.8