Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsItem replace boundingRect with more precise shape

QGraphicsItem replace boundingRect with more precise shape

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 478 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AganDur
    wrote on last edited by
    #1

    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).

    23289f07-c44c-4f1b-bfb3-327198e20f48-image.png

    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

    Pl45m4P JonBJ 2 Replies Last reply
    0
    • A AganDur

      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).

      23289f07-c44c-4f1b-bfb3-327198e20f48-image.png

      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

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @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

      • https://doc.qt.io/qt-6/qgraphicsitem.html#shape

      and re-implement it for your item to make it return a more precise painterPath describing your hexagon. Then you can use

      • https://doc.qt.io/qt-6/qgraphicsitem.html#collidesWithItem

      Is there a way to make itemAt work with a QGraphicsPixmapItem ?

      QGraphicsPixmapItem should also work with itemAtsince 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.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      A 1 Reply Last reply
      2
      • A AganDur

        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).

        23289f07-c44c-4f1b-bfb3-327198e20f48-image.png

        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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @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 QGraphcsItems is probably not the way to go. For such a situation you are better using drawBackground() to show a map with the hexagons drawn on it, and QGraphicsItems only for the objects you place onto the map.

        A 1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @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

          • https://doc.qt.io/qt-6/qgraphicsitem.html#shape

          and re-implement it for your item to make it return a more precise painterPath describing your hexagon. Then you can use

          • https://doc.qt.io/qt-6/qgraphicsitem.html#collidesWithItem

          Is there a way to make itemAt work with a QGraphicsPixmapItem ?

          QGraphicsPixmapItem should also work with itemAtsince 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.

          A Offline
          A Offline
          AganDur
          wrote on last edited by
          #4

          @Pl45m4 said in QGraphicsItem replace boundingRect with more precise shape:

          You can make use of

          • https://doc.qt.io/qt-6/qgraphicsitem.html#shape

          and re-implement it for your item to make it return a more precise painterPath describing your hexagon. Then you can use

          • https://doc.qt.io/qt-6/qgraphicsitem.html#collidesWithItem

          I've looked at those functions and managed to do what I wanted, thank you !

          QGraphicsPixmapItem should also work with itemAtsince 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.

          1 Reply Last reply
          0
          • JonBJ JonB

            @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 QGraphcsItems is probably not the way to go. For such a situation you are better using drawBackground() to show a map with the hexagons drawn on it, and QGraphicsItems only for the objects you place onto the map.

            A Offline
            A Offline
            AganDur
            wrote on last edited by
            #5

            @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 QGraphcsItems is probably not the way to go. For such a situation you are better using drawBackground() to show a map with the hexagons drawn on it, and QGraphicsItems 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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved