所以最近我问了一个问题,看看是否可以在同一个端口上运行多个 WT 应用程序?答案是肯定的(+1 给 Jorge Núñez 的真棒答案)。但是,现在我试图通过将多个 WT 应用程序嵌入到一种主机 WT 应用程序中,将他的解决方案更进一步,看看是否可以在同一页面上运行多个 WT 应用程序。我所做的是创建了一个主机 WT 应用程序,该应用程序具有其 root() 的访问器并具有 WTabWidget。然后在 CreateHostApplication 函数中,我为 2 个测试应用程序创建了选项卡,它们的 root() 也有访问器,并将它们的 root() 添加到属于我的主机应用程序的选项卡中,然后在所有应用程序都添加到它们各自的选项卡之后选项卡,我返回了主机。
很酷的部分是测试应用程序中的小部件如我预期的那样显示在它们的选项卡中,我没想到的是用于将按钮连接到函数的 connect() 调用失败。因此,只要单击它们并在框中编辑文本,这些小部件就可以发挥作用,但它们没有做任何其他事情,因为它们没有连接到任何自定义功能。
在调试了一会儿之后,我很确定测试应用程序上的连接调用失败了,因为它们实际上并不是由浏览器托管的,这将我带到了这里。我一直无法找到解决此问题的方法,有没有办法通过此设置使连接调用正常工作?
下面的代码是 Jorge Núñez 的解决方案,经过上述修改。我正在使用 Visual Studio2010 进行开发。预先感谢您的任何帮助!
#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>
#include <Wt/WException>
#include <Wt/WLogger>
#include <Wt/WServer>
#include <Wt/WTabWidget>
using namespace Wt;
class Host : public Wt::WApplication
{
public:
Host(const Wt::WEnvironment& env);
WContainerWidget* GetRoot()
{
return root();
}
};
Host::Host(const Wt::WEnvironment& env) : Wt::WApplication(env)
{
}
class TestApp1 : public Wt::WApplication
{
public:
TestApp1(const Wt::WEnvironment& env, const std::string& title);
WContainerWidget* GetRoot()
{
return root();
}
private:
Wt::WLineEdit* _name_edit;
Wt::WText* _greeting;
void Greet();
};
TestApp1::TestApp1(const Wt::WEnvironment& env, const std::string& title) : Wt::WApplication(env)
{
setTitle(title);
root()->addWidget(new Wt::WText("Your name, please ? "));
_name_edit = new Wt::WLineEdit(root());
Wt::WPushButton* button = new Wt::WPushButton("Greet me.", root());
root()->addWidget(new Wt::WBreak());
_greeting = new Wt::WText(root());
button->clicked().connect(this, &TestApp1::Greet);
}
void TestApp1::Greet()
{
_greeting->setText("Hello there, " + _name_edit->text());
}
class TestApp2 : public Wt::WApplication
{
public:
TestApp2(const Wt::WEnvironment& env, const std::string& title);
WContainerWidget* GetRoot()
{
return root();
}
private: Wt::WLineEdit *_name_edit;
Wt::WText *_greeting;
void greet();
};
TestApp2::TestApp2(const Wt::WEnvironment& env, const std::string& title) : Wt::WApplication(env)
{
setTitle(title);
root()->addWidget(new Wt::WText("Your name, please ? "));
_name_edit = new Wt::WLineEdit(root());
Wt::WPushButton* button = new Wt::WPushButton("Say goodbye.", root());
root()->addWidget(new Wt::WBreak());
_greeting = new Wt::WText(root());
button->clicked().connect(this, &TestApp2::greet);
}
void TestApp2::greet()
{
_greeting->setText("Goodbye, " + _name_edit->text());
}
Wt::WTabWidget* tab_widget;
Wt::WApplication* CreateHostApplication(const Wt::WEnvironment& env)
{
Host* host = new Host(env);
WContainerWidget* root = host->GetRoot();
tab_widget = new WTabWidget(root);
//Create tab for the app
WContainerWidget* Tab_TestApp1 = new WContainerWidget();
//Get a pointer to the ACE tab
tab_widget->addTab(Tab_TestApp1, "Test Application 1", Wt::WTabWidget::LoadPolicy::PreLoading);
//Create app
TestApp1* test_app_1 = new TestApp1(env, "Test Application 1");
//Add app root to the tab
Tab_TestApp1->addWidget(test_app_1->GetRoot());
//Create tab for the app
WContainerWidget* Tab_TestApp2 = new WContainerWidget();
//Get a pointer to the ACE tab
tab_widget->addTab(Tab_TestApp2, "Test Application 2", Wt::WTabWidget::LoadPolicy::PreLoading);
//Create app
TestApp2* test_app_2 = new TestApp2(env, "Test Application 2");
//Add app root to the tab
Tab_TestApp2->addWidget(test_app_2->GetRoot());
return host;
}
Wt::WApplication* CreateTestApp1(const Wt::WEnvironment& env)
{
return new TestApp1(env, "Test Application 1");
}
Wt::WApplication* CreateTestApp2(const Wt::WEnvironment& env)
{
return new TestApp2(env, "Test Application 2");
}
int TestWRun(int argc, char* argv[],
Wt::ApplicationCreator host_application,
std::vector<Wt::ApplicationCreator> applications)
{
try
{
// use argv[0] as the application name to match a suitable entry
// in the Wt configuration file, and use the default configuration
// file (which defaults to /etc/wt/wt_config.xml unless the environment
// variable WT_CONFIG_XML is set)
Wt::WServer server(argv[0],"");
// WTHTTP_CONFIGURATION is e.g. "/etc/wt/wthttpd"
server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);
// add a single entry point, at the default location (as determined
// by the server configuration's deploy-path)
server.addEntryPoint(Wt::Application, host_application);
unsigned int num_apps = applications.size();
for(unsigned int cur_app = 0; cur_app < num_apps; ++cur_app)
{
server.addEntryPoint(Wt::Application, applications[cur_app], "/" + boost::lexical_cast<std::string>(cur_app));
}
if (server.start())
{
int sig = Wt::WServer::waitForShutdown(argv[0]);
std::cerr << "Shutdown (signal = " << sig << ")" << std::endl;
server.stop();
}
}
catch (Wt::WServer::Exception& e)
{
std::cerr << e.what() << "\n";
return 1;
}
catch (std::exception& e)
{
std::cerr << "exception: " << e.what() << "\n";
return 1;
}
}
int main(int argc, char** argv)
{
std::vector<Wt::ApplicationCreator> applications;
applications.push_back(&CreateTestApp1);
applications.push_back(&CreateTestApp2);
return TestWRun(argc, argv, &CreateHostApplication, applications);
}