Search code examples
pythonpyqtpyqt5qgraphicsscene

PyQt5: All Items of GraphicsScene have coordinates 0.0


I used the following source and modified it a bit, to get the following mini example:

import sys
from PyQt5 import QtCore, QtWidgets

class GraphicsScene(QtWidgets.QGraphicsScene):
    def __init__(self):
        super(GraphicsScene, self).__init__()
        self.setSceneRect(0, 0, 600, 400)


    def mousePressEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:
            x = event.scenePos().x()
            y = event.scenePos().y()
            self.addRect(x, y, 100, 100)
        elif event.buttons() == QtCore.Qt.RightButton:
            for elem in self.items():
                print(elem.x())
        super(GraphicsScene, self).mousePressEvent(event)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    scene = GraphicsScene()
    w = QtWidgets.QGraphicsView(scene)
    w.resize(610, 410)
    w.show()
    sys.exit(app.exec_())

The idea is, to create new rectangles by making left mouse clicks (this works already) and delete the nearest rectangle by making a right mouse click. I know, how I can find the nearest rectangle, but for this I need the coordinates of the existing rectangles. If we add a new rectangle to the scene, we do the following:

self.addRect(x, y, 100, 100)

But if I iterate over all elements in the scene, and try to get the x-coordinate of the elements using this:

    for elem in self.items():
        print(elem.x())
        print(elem.pos().x())
        print(elem.scenePos().x())

then all the print-outputs are zero. I had already a look at the docu, but as I understand it I am doing exactly what the docu recommends. Do you know what I am doing wrong?


EDIT: Of course, I could save all the coordinates in an additional list, compute the nearest rectangle with the values in that list, delete each rectangle by using:

    for elem in self.items():
        self.removeItem(elem)

and plot the remaining rectangles. However, I hope there is a cleaner version for this. :)


Solution

  • As the documentation explains:

    Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0). For example, if a QRect(50, 50, 100, 100) is added, its top-left corner will be at (50, 50) relative to the origin in the item's coordinate system.

    So there are two choices:

    • add a rectangle with the specified size but at position (0, 0), then move it at the required position:
        rectItem = self.addRect(0, 0, 100, 100)
        rectItem.setPos(x, y)
    
    • use the coordinates in addRect and get the actual position based on the top left corner of the rectangle:
        for elem in self.items():
            pos = elem.pos()
            if isinstance(elem, QtWidgets.QGraphicsRectItem):
                pos += elem.rect().topLeft()