Wt (web toolkit)

From Wikipedia, the free encyclopedia
  (Redirected from Wt - Web toolkit)
Jump to: navigation, search
Wt
Original author(s) Emweb
Initial release 1.0.0 / Released December 2005
Stable release
3.3.7 / March 31, 2017; 27 days ago (2017-03-31)[1]
Written in C++
Operating system Any
Type Web framework
License Dual-licensed:
Website www.webtoolkit.eu

Wt (pronounced "witty") is an open-source widget-centric web framework for the C++ programming language. It has an API resembling that of Qt framework, also using a widget-tree and an event-driven signal/slot system.[2]

The Wt's design goal is to benefit from the stateful component model used in desktop-applications APIs, applied to web development—instead of the traditional MVC (model–view–controller) design pattern. So rather than using MVC at the level of a web page, it is pushed to the level of individual components.[3]

While the library uses a desktop software development process, it does support some web-specific features, including:

One of the unique features of Wt is its abstraction layer of the browser rendering model. The library uses Ajax for communicating with browsers compatible with it, while using plain HTML-form post-backs for other user agents. Using a progressive bootstrap-method, the user interface is rendered as a plain HTML document first, then, provided its support in browser, it is automatically upgraded to use Ajax for increased interactivity. In this way, Wt is by definition:

Because of the popularity of C/C++ in embedded system environments, Wt is often used in such devices and (as a consequence) has been highly optimized for performance.

Major features[edit]

For a more detailed overview, see the Features section of official website.

Code example[edit]

The "Hello, World!" program in Wt:

#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>

// c++0x only, for std::bind
// #include <functional>

using namespace Wt;

/*
 * A simple "Hello, World!" application-class demonstrates: 
 * How to 
 * react to events, read input, give feed-back?
 */
class HelloApplication : public WApplication
{
public:
  HelloApplication(const WEnvironment& env);

private:
  WLineEdit *nameEdit_;
  WText *greeting_;

  void greet();
};

/*
 * The "env"-argument contains information about 
 * a new session and an initial request. It must be passed 
 * to the "WApplication"-constructor,--
 * so it is typically also an argument for your custom
 * application-constructor.
*/
HelloApplication::HelloApplication(const WEnvironment& env)
  : WApplication(env)
{
  setTitle("Hello world");                               // application title

  root()->addWidget(new WText("Your name, please? "));   // show some text
  nameEdit_ = new WLineEdit(root());                     // allow text input
  nameEdit_->setFocus();                                 // give focus

  WPushButton *button
    = new WPushButton("Greet me.", root());              // create a button
  button->setMargin(5, Left);                            // add 5 pixels margin

  root()->addWidget(new WBreak());                       // insert a line-break

  greeting_ = new WText(root());                         // empty text

  /*
   * Connect signals with slots
   *
   * - simple "Wt"-way
   */
  button->clicked().connect(this, &HelloApplication::greet);

  /*
   * - using an arbitrary function object (binding values with boost::bind())
   */
  nameEdit_->enterPressed().connect
    (boost::bind(&HelloApplication::greet, this));

  /*
   * - using a c++0x lambda:
   */
  // b->clicked().connect(std::bind([=]() { 
  //       greeting_->setText("Hello there, " + nameEdit_->text());
  // }));
}

void HelloApplication::greet()
{
  /*
   * Update the text, using text-input into the "nameEdit_"-field.
   */
  greeting_->setText("Hello there, " + nameEdit_->text() + "!");
}

WApplication *createApplication(const WEnvironment& env)
{
  /*
   * You could read information from the environment to decide: 
   * Whether the User has a permission to start a new application?
   */
  return new HelloApplication(env);
}

int main(int argc, char **argv)
{
  /* The method may set up some shared resources, 
   * but should then start a server-application ("FastCGI" or "httpd"), to: 
   * 1. Listen for requests; 
   * 2. Handle all of the application's life-cycles.
   *
   * The last "WRun"-argument specifies 
   * a new application-objects instantiating function--
   * to execute when 
   * a new user surfs to the "Wt"-application 
   * and the library has a negotiated browser support. 
   * (This function should return a newly instantiated application-object.)
   */
  return WRun(argc, argv, &createApplication);
}

See also[edit]

References[edit]

  1. ^ Wt Release notes
  2. ^ Dumon, Wim; Deforche, Koen (February 11, 2008). "Wt: A Web Toolkit". Dr. Dobb's Journal. Retrieved January 24, 2017. 
  3. ^ Volkman, Victor (June 6, 2008). "Wt: C++ Web Toolkit Library Lets You Write Scripting-Independent Web Apps". QuinStreet. Retrieved January 24, 2017. 

External links[edit]

Official website