Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTGraphicsItem relative coordinates in a Grid



  • I'm designing a grid system in a QgraphicsView. I've made Tiles using QGraphicsItems which lay across the view, as show below:
    image here
    The issue I'm having is when adding objects to a tile. I'm trying to add a QPixmapItem or some other item (such as a rectangle) to a specific tile, say (3,4) tile in the grid. What is the best way to do this, such that the item is drawn relative to a given tile.

    That is, if I were to say addObject(myObject, x, y) the object will be drawn to fit right in tile x, y, and I can scale it relative to that tile. I was able to make everything work for one grid size; but if I add rows/columns, everything is off center again.



  • Make the respective tile the parentItem of your new GraphicsItem:

    myItem->setParentItem(myTile);

    Now all coordinates of myItem are relative to that respective tile.