Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
This article shows the way to create a TicTacToe widget when using the Wt library.
This article follows the same architecture as Thinking Wt 1: general. First the bare-bone architecture is implemented, so the programmer can test the application as early as possible. Next follows the dialog and widget implementation, ending with a conclusion.
The architecture, from biggest to smallest, consists of:
The purpose of first implementing the bare-bone architecture is to get the program running, so it can be tested. The same architecture as Thinking Wt 1 is used:
#include <boost/signals2.hpp> |
The most notable in this setup is that WtTicTacToeWidget is a derived class of Wt::WPaintedWidget. This is because then there is most control in drawing the TicTacToe board. For now, the widget shows an image called 'R.png'.
Add the following line to your Qt project file (to prevent link errors like undefined reference to 'Wt::WRun(int, char**, Wt::WApplication* (*)(Wt::WEnvironment const&))'):
LIBS += -lwt -lwthttp |
Additionally, add the following line to your Qt project file, as Wt uses the Boost.Signals library, that needs to be linked to as well:
LIBS += -lboost_signals |
Add the following arguments to the Run Settings (to prevent the misc error stat: No such file or directory. Document root ("") not valid.
--docroot . --http-address 0.0.0.0 --http-port 8080 |
Start the program and your favorite webbrowser. Take the webbrowser to the following address:
http://127.0.0.1:8080/ |
In this simple example, the WtTicTacToeDialog shows both a WtTicTacToeWidget and a restart button, aligned vertically. The restart button also shows the state of the game (that is: player 1 has won, player 2 has won, draw, game is still unfinished):
#include <boost/signals2.hpp> |
Note that the widget has only dummy member functions yet, but that the dialog is fully functional. Because I prefer using the same signal/slot system in all my programs, I use the Boost signals instead of the Wt signals. To get the widgets align vertically, I put a Wt::WBreak between the two relevant widgets. The unnamed Wt::WBreak will be deleted by the dialog.
The widget handles the interface between the TicTacToe class and the user's mouse clicks.
#include <boost/signals2.hpp> |
This article described the gradual development of a custom dialog and widget. Using the architecture described in Thinking Wt 1: general, it is possible to have multiple steps-in-between to check if the program still works.
The tool TestTicTacToe contains the polished and slightly extended version of the code in this article.
My next article, Thinking Wt 3: TicTacToe game describes how I implement the WtTicTacToeWidget in a full game.
Thanks Tor Arne Fallingen for notifying me that I omitted linking to Boost.Signals.
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.