0

我在调整 的位置时遇到问题QGraphicsRectItem。当我通过设置某个位置mouseReleaseEvent时,当我退出应用程序并重新启动时,它显示它是相同的位置,但元素已在QGraphicsView.

这是类的样子:

class Slot(QGraphicsRectItem):

    def __init__(self, iface, slot_id, positionX, positionY, width, height):
        super().__init__()
        self.iface = iface #MainWindow
        self.id = slot_id
        self.positionX = positionX
        self.positionY = positionY
        self.width = width
        self.height = height
        self.setPos(self.positionX, self.positionY)
        self.setRect(self.pos().x(), self.pos().y(), self.width, self.height)
        self.blackPen = QPen(Qt.black)
        self.blackPen.setWidth(1)

        self.setFlag(QGraphicsItem.ItemIsMovable, True)
        self.setFlag(QGraphicsItem.ItemIsSelectable, True)


   def mouseReleaseEvent(self, mouseEvent):
        """
        Executed when the mouse is released from the item.
        """
        self.saveReactValue()
        super().mouseReleaseEvent(mouseEvent)

   def saveReactValue(self):
        tree = et.parse(self.iface.file_in_current_directory)
        root = tree.getroot()
        for element in root.iter():
            for child in list(element):
                if child.attrib.get("id") == self.id:
                    child.set("positionX", str(float(self.pos().x())))
                    child.set("positionY", str(float(self.pos().y())))
                    child.set("slot_width", str(float(self.rect().width())))
                    child.set("slot_height", str(float(self.rect().height())))

        xmldata = et.tostring(root, encoding="unicode")
        myfile = open(self.iface.file_in_current_directory, "w")
        myfile.write(xmldata)
        myfile.close()

        print('EMPTY: id: {0}, x: {1}, y: {2}'.format(self.id, self.pos().x(), self.pos().y()))
4

0 回答 0