---
title: "如何利用QTcreator 开发Wt（C++ web toolkit）程序"
author: "mmdev"
date: "2010-12-09T18:18:00Z"
tags: []
slug: "qtcreator-wt-c-web-toolkit"
draft: false
lastmod: "2011-12-10T16:14:34Z"
id: 1301431
url: "https://mmdev.iteye.com/blog/1301431"
---

Wt是什么？ 我不想做过的解释，如果感兴趣的朋友可以去[http://www.webtoolkit.eu/wt](http://www.webtoolkit.eu/wt)看一看，wt是将来嵌入式开发的一种必然趋势，因为wt现在刚刚起步，还没有相对成熟的，设置连自己的一套开发jdk还没有出来，这给开发wt的人员带来了相当大的痛苦，本人也是配置wt的开发环境花了四天的时间，最后放弃了wt官方的资料（实在不敢恭维wt的官方资料），利用qtcreator来开发wt。

从一个简单的helloword开始吧

pro文件

#-------------------------------------------------

\#

\# Project created by QtCreator 2010-11-18T12:10:20

\#

#-------------------------------------------------

QT -= core

QT -= gui

TARGET = hello.wt

LIBS += -L/usr/local/lib -lwt -lwthttp

CONFIG += console

CONFIG -= app\_bundle

TEMPLATE = app

SOURCES += main.cpp

main.cpp

#include &lt;Wt/WApplication&gt;

#include &lt;Wt/WBreak&gt;

#include &lt;Wt/WContainerWidget&gt;

#include &lt;Wt/WLineEdit&gt;

#include &lt;Wt/WPushButton&gt;

#include &lt;Wt/WText&gt;

#include &lt;boost/version.hpp&gt;

using namespace Wt;

/*

\* A simple hello world application class which demonstrates how to react

\* to events, read input, and 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 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(const WEnvironment& env)

: WApplication(env)

{

setTitle("Hello"); // application title

root()-&gt;addWidget(new WText("Your name, please ? ")); // show some text

nameEdit_ = new WLineEdit(root()); // allow text input

nameEdit\_-&gt;setFocus(); // give focus

WPushButton \*b = new WPushButton("Greet me.", root()); // create a button

b-&gt;setMargin(5, Left); // add 5 pixels margin

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

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

/*

\* Connect signals with slots

\*

\* - simple Wt-way

\*/

b-&gt;clicked().connect(this, &HelloApplication::greet);

/*

\* - using an arbitrary function object (binding values with boost::bind())

\*/

nameEdit\_-&gt;enterPressed().connect

(boost::bind(&HelloApplication::greet, this));

}

void HelloApplication::greet()

{

/*

\* Update the text, using text input into the nameEdit_ field.

\*/

greeting\_-&gt;setText("Hello there, " + nameEdit\_-&gt;text());

}

WApplication \*createApplication(const WEnvironment& env)

{

/*

\* You could read information from the environment to decide whether

\* the user has permission to start a new application

\*/

return new HelloApplication(env);

}

int main(int argc, 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.

\*/

return WRun(argc, argv, &createApplication);

}

这就是helloword 的工程文件

现在编译肯定是无法运行的

![](http://hi.csdn.net/attachment/201012/9/3619941_12918894214RWx.jpg)

然后在运行环境中添加wt的共享库地址 好了 现在打开浏览器 输入0.0.0.0:8080 点击编译运行

成功 ，这样开发起来效率不知道高出n倍吧，诶，不知道wt 什么时候才能在国内普及呢

![](http://hi.csdn.net/attachment/201012/9/3619941_12918898042wVj.jpg)