0

I'm trying to allow the user to be able to alter the points of a shape. If I create one Rectangle I'm able to alter each point however, the second I add another Rectangle It only alters the points of the first shape and moves the first shape over the new Rectangle.

    for (int i = 0; i < ShapePointsOnly.count(); i++) {
        int pointShapeX = ShapePointsOnly.at(i).x();
        int pointShapeY = ShapePointsOnly.at(i).y();

        if (mouseX >= pointShapeX - 10 && mouseX <= pointShapeX + 10) {
            if (mouseY >= pointShapeY - 10 && mouseY <= pointShapeY + 10) {
                tempValue.setX(pointShapeX);
                tempValue.setY(pointShapeY);
                selectedPoint = ShapePointsOnly.indexOf(tempValue);
                userCanChangePoints = true;
                return true;
            }
        }
    }

    QBrush noBrush(Qt::NoBrush);
    QPen blackPen(Qt::black);
    blackPen.setWidth(0);

    Rectangle.append(QPointF(300, 300));
    Rectangle.append(QPointF(300, 400));
    Rectangle.append(QPointF(400, 400));
    Rectangle.append(QPointF(400, 300));

    RectangleConverted = Rectangle.toPolygon();
    scene -> addPolygon(RectangleConverted);
    numberOfShapesAdded = numberOfShapesAdded + 1;
    addShapesToList();
Jooshhy
  • 1
  • 1
  • Concerning _and moves the first shape over the new Rectangle_: `scene -> addPolygon(RectangleConverted);` i.e. the new shape is added to the end. But, you didn't remove the rectangle (the shape is converted from), did you? If you don't want that the shape appears over all the other rectangles then you should remember the index of the rectangle and insert the shape at this index. – Scheff's Cat Apr 25 '20 at 08:20
  • That said, _insert the shape at this index_ is not as trivial as I expected. After reading the doc. a bit, I found the following possible work-around: [GraphicsItem::stackBefore()](https://doc.qt.io/qt-5/qgraphicsitem.html#stackBefore). This would require additionally to add an extra item as "root item" which becomes the container of all your rects and shapes. (I missed to find a `stackBefore()` in `QGraphicsScene` though I saw other possible candidates I didn't investigate further.) – Scheff's Cat Apr 25 '20 at 08:31

0 Answers0