Wt examples
4.11.3
hello
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
*/
18
class
HelloApplication
:
public
Wt::WApplication
19
{
20
public
:
21
HelloApplication
(
const
Wt::WEnvironment
& env);
22
23
private
:
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
*/
36
HelloApplication::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
74
void
HelloApplication::greet
()
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
std::make_unique<HelloApplication>(env);
101
});
102
}
HelloApplication
Definition:
hello.C:19
HelloApplication::HelloApplication
HelloApplication(const Wt::WEnvironment &env)
Definition:
hello.C:36
HelloApplication::greeting_
Wt::WText * greeting_
Definition:
hello.C:25
HelloApplication::greet
void greet()
Definition:
hello.C:74
HelloApplication::nameEdit_
Wt::WLineEdit * nameEdit_
Definition:
hello.C:24
Wt::EventSignal::connect
Wt::Signals::connection connect(F function)
Wt::WApplication
Wt::WApplication::root
WContainerWidget * root() const
Wt::WApplication::setTitle
void setTitle(const WString &title)
Wt::WContainerWidget::addWidget
virtual void addWidget(std::unique_ptr< WWidget > widget)
Wt::WEnvironment
Wt::WInteractWidget::enterPressed
EventSignal & enterPressed()
Wt::WLineEdit
Wt::WLineEdit::text
const WString & text() const
Wt::WText
Wt::WText::setText
bool setText(const WString &text)
Wt::WWebWidget::setFocus
virtual void setFocus(bool focus) override
main
int main(int argc, char **argv)
Definition:
hello.C:82
Wt::Side::Left
@ Left
Generated on Wed Feb 19 2025 for
the C++ Web Toolkit (Wt)
by
1.9.1