QGraphicsItem replace boundingRect with more precise shape
-
Hey there,
I have a few questions concerning the Graphics Framework on Qt, as I have ran into a few issues.
I'm making a hexagon-shaped map, in the style of hexcrawler games and such, and have a few doubts.
- First off, I would like to know how best to handle selecting the right hexagon in the scene.
Using functions such as QGraphicsScene::itemAt() seems to use the object's boundingRect to handle this, which is be problematic : my hexes touch, and while the main part (hex itself) does not overlap, the current boundingRects definitely do, meaning that I sometimes select the wrong hex because of that overlapping area (see picture).
My first question is: is there any way to use a more precise tool, such as the boundingRegion or shape, and keep using the already defined functions like itemAt ? If not, I guess I could make the boundingRect smaller (fit the 4 square corners for example) but that seems like it should not be done.
- Then, I have a problem with the hexagon itself.
Currently, it is made by hand as a custom QGraphicsItem (with a few drawLine calls in an overriden paint function).
Before that, I was using a custom QGraphicsPixmapItem, but it seems like itemAt was not working with those. The only difference that I used a Pixmap to draw instead of drawLine, the other functions (boundingRect, paint, and the position calculation before calling itemAt) were the exact same.
Is there a way to make itemAt work with a QGraphicsPixmapItem ?
Thanks in advance !
Agan -
Hey there,
I have a few questions concerning the Graphics Framework on Qt, as I have ran into a few issues.
I'm making a hexagon-shaped map, in the style of hexcrawler games and such, and have a few doubts.
- First off, I would like to know how best to handle selecting the right hexagon in the scene.
Using functions such as QGraphicsScene::itemAt() seems to use the object's boundingRect to handle this, which is be problematic : my hexes touch, and while the main part (hex itself) does not overlap, the current boundingRects definitely do, meaning that I sometimes select the wrong hex because of that overlapping area (see picture).
My first question is: is there any way to use a more precise tool, such as the boundingRegion or shape, and keep using the already defined functions like itemAt ? If not, I guess I could make the boundingRect smaller (fit the 4 square corners for example) but that seems like it should not be done.
- Then, I have a problem with the hexagon itself.
Currently, it is made by hand as a custom QGraphicsItem (with a few drawLine calls in an overriden paint function).
Before that, I was using a custom QGraphicsPixmapItem, but it seems like itemAt was not working with those. The only difference that I used a Pixmap to draw instead of drawLine, the other functions (boundingRect, paint, and the position calculation before calling itemAt) were the exact same.
Is there a way to make itemAt work with a QGraphicsPixmapItem ?
Thanks in advance !
Agan@AganDur said in QGraphicsItem replace boundingRect with more precise shape:
My first question is: is there any way to use a more precise tool, such as the boundingRegion or shape, and keep using the already defined functions like itemAt?
You can make use of
and re-implement it for your item to make it return a more precise painterPath describing your hexagon. Then you can use
Is there a way to make itemAt work with a QGraphicsPixmapItem ?
QGraphicsPixmapItem
should also work withitemAt
since it also has a boundingRect and if a given point is within the boundingRect, it should lead to your pixmap. But Pixmaps also can be only rects. -
Hey there,
I have a few questions concerning the Graphics Framework on Qt, as I have ran into a few issues.
I'm making a hexagon-shaped map, in the style of hexcrawler games and such, and have a few doubts.
- First off, I would like to know how best to handle selecting the right hexagon in the scene.
Using functions such as QGraphicsScene::itemAt() seems to use the object's boundingRect to handle this, which is be problematic : my hexes touch, and while the main part (hex itself) does not overlap, the current boundingRects definitely do, meaning that I sometimes select the wrong hex because of that overlapping area (see picture).
My first question is: is there any way to use a more precise tool, such as the boundingRegion or shape, and keep using the already defined functions like itemAt ? If not, I guess I could make the boundingRect smaller (fit the 4 square corners for example) but that seems like it should not be done.
- Then, I have a problem with the hexagon itself.
Currently, it is made by hand as a custom QGraphicsItem (with a few drawLine calls in an overriden paint function).
Before that, I was using a custom QGraphicsPixmapItem, but it seems like itemAt was not working with those. The only difference that I used a Pixmap to draw instead of drawLine, the other functions (boundingRect, paint, and the position calculation before calling itemAt) were the exact same.
Is there a way to make itemAt work with a QGraphicsPixmapItem ?
Thanks in advance !
Agan@AganDur said in QGraphicsItem replace boundingRect with more precise shape:
I'm making a hexagon-shaped map, in the style of hexcrawler games
I don't know what "hexcrawl" games are. But if you mean you have some kind of map, and then you draw hexagons on it, and perhaps then you add "objects" or "counters" onto it which you move around, then representing the map as individual hexagon
QGraphcsItem
s is probably not the way to go. For such a situation you are better usingdrawBackground()
to show a map with the hexagons drawn on it, andQGraphicsItems
only for the objects you place onto the map. -
@AganDur said in QGraphicsItem replace boundingRect with more precise shape:
My first question is: is there any way to use a more precise tool, such as the boundingRegion or shape, and keep using the already defined functions like itemAt?
You can make use of
and re-implement it for your item to make it return a more precise painterPath describing your hexagon. Then you can use
Is there a way to make itemAt work with a QGraphicsPixmapItem ?
QGraphicsPixmapItem
should also work withitemAt
since it also has a boundingRect and if a given point is within the boundingRect, it should lead to your pixmap. But Pixmaps also can be only rects.@Pl45m4 said in QGraphicsItem replace boundingRect with more precise shape:
You can make use of
and re-implement it for your item to make it return a more precise painterPath describing your hexagon. Then you can use
I've looked at those functions and managed to do what I wanted, thank you !
QGraphicsPixmapItem
should also work withitemAt
since it also has a boundingRect and if a given point is within the boundingRect, it should lead to your pixmap. But Pixmaps also can be only rects.I will have to look at it again then, I must have missed something the first time around.
-
@AganDur said in QGraphicsItem replace boundingRect with more precise shape:
I'm making a hexagon-shaped map, in the style of hexcrawler games
I don't know what "hexcrawl" games are. But if you mean you have some kind of map, and then you draw hexagons on it, and perhaps then you add "objects" or "counters" onto it which you move around, then representing the map as individual hexagon
QGraphcsItem
s is probably not the way to go. For such a situation you are better usingdrawBackground()
to show a map with the hexagons drawn on it, andQGraphicsItems
only for the objects you place onto the map.@JonB said in QGraphicsItem replace boundingRect with more precise shape:
I don't know what "hexcrawl" games are. But if you mean you have some kind of map, and then you draw hexagons on it, and perhaps then you add "objects" or "counters" onto it which you move around, then representing the map as individual hexagon
QGraphcsItem
s is probably not the way to go. For such a situation you are better usingdrawBackground()
to show a map with the hexagons drawn on it, andQGraphicsItems
only for the objects you place onto the map.Actually, I am not going to be moving many objects around (maybe only one or two). The selection of hexes will be to access additional information to that shown by the hex's visual content (a small string + a couple of simple shapes). I thought about using a simple background, as you suggest, but felt like I would then have to rely on using the selection on smaller items, which did not seem ideal.