9.6.3. Exercises: Layout of Widgets

  1. In Designer, try to reproduce a layout of buttons resembling that in Figure 9.14.

      solution/layout/buttonrows/buttonrows.ui

  2. The 15 puzzle (or n2–1 puzzle) involves an 4x4 (nxn) grid that contains 15 tiles numbered 1 to 15 (1 to n2–1), and one empty space. The only tiles that can move are those next to the empty space.

    • Create a 15 puzzle with QPushButtons in a QGridLayout.

    • At the start of the game, the tiles are presented to the player in "random" order. The object of the game is to rearrange them so that they are in ascending order, with the lowest numbered tile in the upper-left corner.

    • If the player solves the puzzle, pop up a QMessageBox saying "YOU WIN!" (or something more clever).

    • Add some buttons:

      • Shuffle – Randomize the tiles, by performing a large number (at least 50) of legal tile-slides.

      • Quit – Terminate the game.

    Figure 9.15.  Sample 15 Puzzle

    Sample 15 Puzzle


[ fromfile: layout.xml id: x-layouts ]

[Tip] QButtonGroup

To avoid connecting a signal to a slot for each button, you can group the buttons together in a QButtonGroup, which has a 1-argument signal for QAbstractButton.

[Tip] Design Suggestions

Start with classes shown in Figure 9.16 and try to partition your code properly into them. The "view" classes should deal with GUI while the "model" classes should have no QWidget code or dependency at all, and deal only with the logic of the game.

Figure 9.16.  Model-View-Controller Design for Puzzle

Model-View-Controller Design for Puzzle

  solution/15-puzzle