PDA

View Full Version : Puzzle Example on Qt's website



yakuzan
27th May 2010, 04:16
Sup guys, I'm working on a project and it shares some similarities with the puzzle that's in the QT doc's:

http://doc.trolltech.com/4.5/itemviews-puzzle.html

Anyways, I've understood most of the code but something has been bugging me:

Is the puzzleWidget class the whole puzzle or a single puzzle piece? I'm confused since in the constructor the puzzleWidget is declaring a size of 400 by 400, which is the size of the puzzle(5 by 5 pieces of 80px's length).

But, I haven't been able to find the point where the widget separates the pieces into a regular board. There's no gridlayout and the two loops inside the .cpp don't seem to create each individual pieces.

In short, I'm trying to understand how they managed to make the grid.

thanks

tbscope
27th May 2010, 05:43
There isn't a QGridLayout used, but I assume you already know that.

The widget is devided into several target squares, each 80x80.
A position is calculated by using the piece location. This is done with the following function:


const QRect PuzzleWidget::targetSquare(const QPoint &position) const
{
return QRect(position.x()/80 * 80, position.y()/80 * 80, 80, 80);
}

If you look at the code, you'll see several lists, one for the pieces themselves, one for the position, etc...
When you drop a piece, the location and the piece are added to these lists. Then when you select a piece and move it around, this location is used to calculate if you're on the target rect or not.