Two questions of my recent project
Added by ivan jobs over 14 years ago
Hi guys,
Recently I've been working on a word reciter project. I made the decision to use Wt but it brought me a lot of trouble. Please help me. Thanks.
My main codes are below:
main.cpp:
@
#include
#include
#include
#include
#include
#include
#include <boost/version.hpp>
#include "wordreciter.h"
//using namespace Wt;
/*
- 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.
*/
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 WordReciter(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);
}@
WordReciter.h & WordReciter.cpp:
@#ifndef WORDRECITER_H
#define WORDRECITER_H
#include
#include
#include
#include
#include
using namespace Wt;
#include "wtdialog.h"
#include
using namespace std;
class WordReciter : public WApplication
{
public:
WordReciter(const WEnvironment& env);
private:
WtDialog *dialog;
};
#endif // WORDRECITER_H
#include
#include "wordreciter.h"
WordReciter::WordReciter(const WEnvironment& env)
: WApplication(env)
{
setTitle("Word Reciter");
this->useStyleSheet("wr.css");
dialog = new WtDialog();
root()->addWidget(dialog);
}@
WtDialog.h & WtDialog.cpp:
@
#ifndef WTDIALOG_H
#define WTDIALOG_H
#include
#include
#include
using namespace Wt;
class WtDialog : public WContainerWidget
{
public:
WtDialog();
private:
WMenu *menu;
WStackedWidget *contents;
};
#endif // WTDIALOG_H
#include "wtdialog.h"
#include "introwidget.h"
#include "loginwidget.h"
#include "registerwidget.h"
#include "trainingwidget.h"
#include "leakagewidget.h"
#include
WtDialog::WtDialog()
{
contents = new WStackedWidget();
menu = new WMenu(contents, Wt::Vertical);
this->addWidget(menu);
this->addWidget(contents);
IntroWidget *introWidget = new IntroWidget();
LoginWidget *loginWidget = new LoginWidget();
TrainingWidget *trainingWidget = new TrainingWidget();
RegisterWidget *registerWidget = new RegisterWidget();
LeakageWidget *leakageWidget = new LeakageWidget();
//add items using the default lazy loading policy
menu->addItem("Introduction", introWidget, WMenuItem::LazyLoading);
menu->addItem("Register", registerWidget, WMenuItem::LazyLoading);
menu->addItem("Login", loginWidget, WMenuItem::LazyLoading);
menu->addItem("Training", trainingWidget, WMenuItem::LazyLoading);
menu->addItem("Leakage", leakageWidget, WMenuItem::LazyLoading);
menu->select(3);
}@
As you can see from WtDialog.h & WtDialog.cpp, I deleloped a widget for each page, like "introduction" page, "register" page and "login" page.
You can see every page for a widget:
#include "introwidget.h"
#include "loginwidget.h"
#include "registerwidget.h"
#include "trainingwidget.h"
#include "leakagewidget.h"
And then I use WMenu and WStackedWidget to orgnize these pages (or widgets).
My questions are below:
1.For example if I want to go to trainingwidget page form now loginwidget page , what should I do?
I know that WMenu has a member function select(), but I need to get the instance of WMenu, How can I get it to solve my problem. Or maybe you see what a foolish thing I did, please point it out for me, thanks so so much.
2.If I want to do something when the WMenu's selected Item changed, what should I do?
Thanks for your patience. :)
Replies (1)
RE: Two questions of my recent project - Added by Koen Deforche about 14 years ago
Hey,
Eventually to change page you need to call menu->select() as you suggest. You can do that directly in response to an event (e.g. button clicked()) or indirectly by using WMenu's internal path logic and with a WAnchor which points to an internal path using setInternalPath().
When you do it in response to an event you indeed need to have a reference to the menu object. This is possible by e.g. exposing this menu in your application object and accessing this object using WApplication::instance().
in WordReciter class: keep the menu as an instance variable and provice an accessor ("getter") method:
WMenu *menu() const { return menu_; }
And then from within an event
((WordReciter *) WApplication::instance())->menu()->select(2);
Regards,
koen