What is wrong with this attempt to visualize rotated ellipses in Qt?

1. Purpose

My colleague and I tried to make rotated ellipsoids in Qt. A typical approach to the solution, as we understand it, is to shift the center of the ellipsoids to the origin of the system, rotate there and shift back: http://qt-project.org/doc/qt-4.8/qml-rotation.html

2. Code Example

Based on the solution described in the link above, we came to the following code sample:

// Constructs and destructors RIEllipse(QRect rect, RIShape* parent, bool isFilled = false) : RIShape(parent, isFilled), _rect(rect), _angle(30) {} // Main functionality virtual Status draw(QPainter& painter) { const QPen& prevPen = painter.pen(); painter.setPen(getContColor()); const QBrush& prevBrush = painter.brush(); painter.setBrush(getFillBrush(Qt::SolidPattern)); // Get rectangle center QPoint center = _rect.center(); // Center the ellipse at the origin (0,0) painter.translate(-center.x(), -center.y()); // Rotate the ellipse around its center painter.rotate(_angle); // Move the rotated ellipse back to its initial location painter.translate(center.x(), center.y()); // Draw the ellipse rotated around its center painter.drawEllipse(_rect); painter.setBrush(prevBrush); painter.setPen(prevPen); return IL_SUCCESS; } 

As you can see, in this test sample we hard-coded the angle of rotation by 30 degrees.

3. Observations

Ellipses go to the wrong positions, often outside the canvas area.

4. Question

What is wrong with the code example above?

Yours faithfully,

Baldur

PS Thanks in advance for the constructive answer?

PPS Before sending this message, we searched approximately on stackoverflow.com. The motion / rotation of the Qt image seemed to reflect a solution approach similar to the link above.

+4
source share
2 answers

In painter.translate(center.x(), center.y()); you change your object by the value of the current coordinate, which as a result does (2*center.x(), 2*center.y()) . You may need:

 painter.translate(- center.x(), - center.y()); 
0
source

The theory of moving an object back to its beginning, rotation, and then replacing the position of the object is correct. However, the code you presented is not a translation and rotation of the object in general, but a translation and rotation of the painter. In the example you have indicated, they want to rotate the whole image around the object, so they move the artist to the center of the object before rotation.

The easiest way to rotate around a GraphicsItem is to first define an element centered in the center of the object, and not in the upper left corner. Thus, any rotation will automatically touch the center of the objects, without the need to translate the object.

To do this, you must define an element with a bounding box for x, y, width, height with (-width / 2, -height / 2, width, height).

Alternatively, if your object inherits from QGraphicsItem or QGraphicsObject, you can use the setTransformOriginPoint function before any rotation.

0
source

Source: https://habr.com/ru/post/1487791/


All Articles