PDA

View Full Version : QTableView for hexagon map?



WinchellChung
4th August 2007, 22:27
I'm writing a Qt4 application that plays a traditional "war game." These games are played on a map that is tessellated into hexagons (http://www-cs-students.stanford.edu/~amitp/gameprog.html#hex), instead of squares like in chess.

Now, if the game map is stored as a a huge graphic graphic image, the memory requirements are excessive. To reduce the memory usage, "tiles" are used. This exploits the fact that many of the game hexagons are identical. By analogy, if one wanted to store a chess board image, one would only have to store an image of a black square and a white square. The visible part of the chessboard could be drawn on the fly, then the playing pieces are drawn on top.

Now, topographically, a hex grid is more or less equivalent to a square grid, if alternating columns are offset downward (http://www-cs-students.stanford.edu/~amitp/game-programming/grids/) by half a square height. The pattern resembles bricks in a brick wall.

I was going to implement the hex map display by using a graphic view with two scroll bars.

However, the though occurs to me that it might be possible to subclass a QTableView to do the same thing. One would have to alter the painting and selection functions so alternate columns were offset.

Would it make more sense to implement such a map with a QGraphicView, a QTableView, or some other widget?

marcel
4th August 2007, 22:43
I think QGraphicsView is definitely the way to go here.
Creating a custom QTableView means not only rewriting the painting but also the widget interaction part, which could be a nightmare.

What is wrong displaying the hexagons in a gview? You could still minimize memory usage by storing hexagon types as pixmaps and just use QGraphicsPixmapItem's with a clipping mask applied(a hexagon) to define its bounds.

Regards

WinchellChung
5th August 2007, 01:33
Ihat is wrong displaying the hexagons in a gview? You could still minimize memory usage by storing hexagon types as pixmaps and just use QGraphicsPixmapItem's with a clipping mask applied(a hexagon) to define its bounds.
Nothing is wrong with using a gview. But as I learn Qt I find it often has easier ways to do common tasks. So I just wanted to make sure that I wasn't doing things the hard way.

I actually have hex map drawing code that I wrote for Microsoft Foundation Classes many years ago, it should be easy enough to adapt.

I was bemused, however. The original program in MFC took me the better part of a week to get the user interface working. In PyQt4 with the Qt Designer, it took less than 15 minutes.