| Wt | |
|---|---|
| Original author | Emweb |
| Initial release | 1.0.0 / December 2005; 20 years ago (2005-12) |
| Stable release | |
| Written in | C++ |
| Operating system | Cross-platform |
| Type | Web framework |
| License | Dual-licensed: |
| Website | www |
| Repository | github |
Wt (pronounced"witty") is anopen-sourcewidget-centricweb framework for theC++ programming language. It has anAPI resembling that ofQt framework (although it was developed with Boost, and is incompatible when mixed with Qt), also using a widget-tree and anevent-drivensignal/slot system.[3]
The Wt's design goal is to benefit from thestatefulcomponent model used in desktop-applications APIs, applied toweb development—instead of the traditionalMVC (model–view–controller) design pattern. So rather than using MVC at the level of aweb page, it is pushed to the level of individual components.[4]
While thelibrary uses a desktopsoftware development process, it does support someweb-specific features, including:
One of the unique features of Wt is its abstraction layer of the browserrendering model. The library usesAjax for communicating with browsers compatible with it, while using plainHTML-form post-backs for otheruser agents. Using a progressive bootstrap-method, theuser interface is rendered as a plain HTMLdocument first, then, provided its support in browser, it is automatically upgraded to useAjax for increased interactivity. In this way, Wt is by definition:
Because of the popularity ofC/C++ inembedded system environments, Wt is often used in such devices and (as a consequence) has been highly optimized forperformance.
For a more detailed overview, see the Features section of official website.
The"Hello, World!" program in Wt:
#include<Wt/WApplication.h>#include<Wt/WBreak.h>#include<Wt/WContainerWidget.h>#include<Wt/WLineEdit.h>#include<Wt/WPushButton.h>#include<Wt/WText.h>/* * A simple hello world application class which demonstrates how to react * to events, read input, and give feed-back. */classHelloApplication:publicWt::WApplication{public:HelloApplication(constWt::WEnvironment&env);private:Wt::WLineEdit*nameEdit_;Wt::WText*greeting_;voidgreet();};/* * The env argument contains information about the new session, and * the initial request. It must be passed to the WApplication * constructor so it is typically also an argument for your custom * application constructor.*/HelloApplication::HelloApplication(constWt::WEnvironment&env):WApplication(env){setTitle("Hello world");// application titleroot()->addNew<Wt::WText>("Your name, please ? ");// show some textnameEdit_=root()->addNew<Wt::WLineEdit>();// allow text inputnameEdit_->setFocus();// give focusautobutton=root()->addNew<Wt::WPushButton>("Greet me.");// create a buttonbutton->setMargin(5,Wt::Side::Left);// add 5 pixels marginroot()->addNew<Wt::WBreak>();// insert a line breakgreeting_=root()->addNew<Wt::WText>();// empty text/* * Connect signals with slots * * - simple Wt-way: specify object and method */button->clicked().connect(this,&HelloApplication::greet);/* * - using an arbitrary function object, e.g. useful to bind * values with std::bind() to the resulting method call */nameEdit_->enterPressed().connect(std::bind(&HelloApplication::greet,this));/* * - using a lambda: */button->clicked().connect([=](){std::cerr<<"Hello there, "<<nameEdit_->text()<<"\n";});}voidHelloApplication::greet(){/* * Update the text, using text input into the nameEdit_ field. */greeting_->setText("Hello there, "+nameEdit_->text());}intmain(intargc,char**argv){/* * Your main method may set up some shared resources, but should then * start the server application (FastCGI or httpd) that starts listening * for requests, and handles all of the application life cycles. * * The last argument to WRun specifies the function that will instantiate * new application objects. That function is executed when a new user surfs * to the Wt application, and after the library has negotiated browser * support. The function should return a newly instantiated application * object. */returnWt::WRun(argc,argv,[](constWt::WEnvironment&env){/* * You could read information from the environment to decide whether * the user has permission to start a new application */returnstd::make_unique<HelloApplication>(env);});}