开发者

QGraphicsScene and QGraphicsWidget (reusable and embeddable)

I should start by saying I've tried to look at this problem from all POVs. I've also asked on freenode and I was suggested some things, yet in the end it turned out those suggestions could not possibly work.

It looked to me as though those making suggestions don't really know how the class hierarchy and the concepts hierarchy looks like.

That being said, I haven't used Qt for years, so please bear with me.

What I want to make is a graphics item (QGraphicsWidget,...Item, whatever, I don't know what class it should inherit from) that can sit on the scenery QGraphicsScene.

Imagine you wanted to create a reusable component for grid-based games like battleship.

New objects (like a battleship) within this widget could be represented as bit matrices and created/inserted on the fly.

The building blocks themselves (filled QGraphicsRectItem or "empty" ones) inside it should be managed by a layout class. As a default could serve QGraphicsGridLayout, but it should allow plugging in a custom layout. This would allow new 2D layouts, like isometric.

Having a layout inside the widget would also simplify game development on top of this widget, as you'll have methods itemAt() and 开发者_开发百科layers to organize the objects on the "grid".

And remember, the widget itself can sit inside the scenery.

So which concepts (grid widget, grid block, grid layout) should inherit from which qt4 (4.7.2) classes?

Note: already exiting classes/frameworks/whatever are also OK, but keep in mind I don't want to have to distribute libraries thicker than 3-4 Mb for windows users.


Based on your description, I would recommend to implement the three basic concepts as:

1) "grid" widget inherits from QGraphicsWidget as it supports adding layouts. If you go along with QGraphicsItem, you will need to re implement the layout mechanism which is really painful. The grid creates "grid" block depending on the "grid" shape provided by the "grid" layout.

2) "grid" layout inherits from QGraphicsLayout. You should be able to almost directly use the QGraphicsGridLayout to make simple square grid games. You will have to create new subclasses to handle other kind of grid shapes. One distinct feature you should have in this class is the definition of the shape of a grid block (square, hexagon, diamond). This definition given as a QPolygon can be used to create the correctly shaped grid block.

3) "grid" block inherits from QGraphicsPolygonItem and QGraphicsLayoutItem. This is the more general approach to defined a grid item as a polygon. You could either have rectangle shape, diamond shape, hexagonal shape, etc.. If you want you grid object to be disk arranged in a square grid fashion (see Sane game for instance), you can create a special "grid" block item that contains (as a child) a QGraphicsEllipseItem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜