Creating a widget for the tic-tac-toe board
Let's move on to implementing the board. It should contain nine buttons that can display "X" or "O" and allow the players to make their moves. We could add the button directly to the empty widget of our form. However, the behavior of the board is fairly separate from the rest of the form, and it will have quite a bit of logic inside. Following the encapsulation principle, we prefer implementing the board as a separate widget class. Then, we'll replace the empty widget in our main window with the board widget we created.
Choosing between designer forms and plain C++ classes
One way of creating a custom widget is by adding a Designer Form Class
to the project. Designer Form Class
is a template provided by Qt Creator. It consists of a C++ class that inherits QWidget
(directly or indirectly) and a designer form (.ui
file), tied together by some automatically generated code. Our MainWindow
class also follows this template.
However, if you try to use the...