When you see the graphics View framework, you are completely confused by the concepts of window, viewport, scene coordinate system, object coordinate system, world coordinate system, logical coordinates, physical coordinates and so on. What is the relationship between them? How does it map? Until now the wolf still did not understand, but after continuous testing, a little bit of their own understanding.
The various draw methods of Qpainter are based on the window coordinate system.
The window coordinates are logical coordinates, are based on the viewport coordinate system, and the viewport coordinates are physical coordinates, based on the drawing device coordinate system. In the case of no change, they are the same, they are the size of the drawing device (paint device,qwidget,qpixmap, etc.), the upper left corner is the origin (0,0).
window:
The window represents the area of the viewport, and he always maps the final target with viewport coordinates (the meaning of the sentence will be said again when the viewport is spoken), his size and logical position can be QPainter::setWindow()
set, but regardless of the size and logical location set why the value, he always represents the entire viewport.
For example, if you have a window with an actual size of 200x200 pixels, then the window size under the original state is also 200x200, the viewport size is also 200x200, and when you draw a rectangle with a size of 100x100 in the 0,0 position, he will occupy one of the 4 points in the upper left corner of the viewport.
painter.drawRect(0,0,100,100);
If we change the window position and size by Qpainter::setwindow, for example setWindow(-50,-50,100,100)
Function Prototypes:
void Qpainter::setwindow (int x, int y, int width, int height)
parameters:
x : Top left corner of window x coordinate
y: Window Upper-left corner y-coordinate
height: Window height
The window represents the entire viewport, but the values of the mappings are different, when the logical coordinates ( -50,-50) of the window become the viewport coordinates (0,0), and the logical size of the window becomes the unit length of the 100x100 ( The unit length is used here because the length of the window is not fixed and is affected by the viewport size, because 100 units of length represent the original physical size of 200 pixels, so each unit length is the actual 2 pixels . Because the Qpainter is based on the window coordinates, this time draw a position of ( -50,-50), the size of the rectangle is 50,50.
painter.drawRect(-50,-50,50,50);
The rectangle still occupies one of the 4 points in the upper-left corner of the window (left), and
painter.drawRect(0,0,50,50);
The rectangle occupies one of the 4 points in the lower right corner of the window.
And the viewport corresponds to the physical coordinates, no changes in the case, the viewport size and plot area size, the above example, the viewport properties have not changed, so the upper left corner of the viewport is still in the plot area of the physical location (0,0), in the window coordinates ( -50,-50). Size is a physical size
200x200 pixels, while the 100x100 unit length is in the window coordinate system.
Viewport:
Now let's look at the effect of setting the viewport on the drawing, for the sake of simplicity, first setWindow()
comment out the above sentence, that is, now the window, the viewport is the same.
Now to change the properties of the viewport, first use thepainter.setViewPort(0,0,100,100);
Function Prototypes:
void QPainter::setViewport ( int x, int y, int width, int height )
Parameters:
X: Set the x-coordinate of the upper-left corner of the viewport
Y:: Set the y-coordinate of the upper-left corner of the viewport
Width: Set viewport length
Height: Set viewport width
So the job of the above statement is to set the origin position of the viewport to the origin of the drawing device (here is Qdialog), and the size changes to 100,100.
So what's the situation now?
Now we set the viewport's coordinates to the upper-left corner (0,0) position of the plot area, which is set to half the plot area, because the plot area is (200x200), and we set the viewport to (100x100). That is, now the actual plot area is one of the 4 points in the upper left corner of the drawing device .
Then we'll use it again.
painter.drawRect(0,0,100,100);
Draw a rectangle, what is the actual display? See:
It is one of the 16 points of dialog, why is it so?
We talked about the window coordinates are always mapped with viewport coordinates for the final target, and the original window with no modified properties is the origin point in the upper left corner, the size is 200x200 unit length, we modify the viewport size to 100x100 pixels , The 200 unit length of the window is mapped to 100 pixels of the viewport length, that is, each unit length is 0.5 pixels, so the result is 100x0.5=50 pixels, so the length and height are one of the 4 points of the dialog, the area is 16 points.
What happens if we drag the dialog boundary to change the size of the dialog? The original idea of the bear was that the rectangle should still be one of the 16 of the total size, which is actually wrong.
void Lang::p aintevent (qpaintevent *)
{
qpainter painter (this);
qDebug()<<"after drag:"<<endl; qDebug()<<painter.viewport().width(); qDebug()<<painter.viewport().height(); painter.setViewport(0, 0, 100 ,100 ); //painter.setWindow(-50,-50,100,100); painter.drawRect(0,0, 100, 100);}
The viewport is reset to the actual size of the drawing device before getting the current actual viewport size (paintevent) by Painter.viewport (). Width () and heigth (). For example, when we drag dialog to 400x400 size, the rectangle becomes smaller.
Actually, it's very simple. Because the window values are also reset to dialog (drawing device) size before paintevent, the window size is 400x400 unit length, and the viewport is set to 100x100 pixels, So this time the window size of a unit length for the actual 100/400=0.25 pixels, so draw a 100x100 unit length of the rectangle, the actual large hours 25x25 pixels, so become smaller.
I am using QQ function to measure, QQ will give the current size of the interception of graphics, it is 25x25 (when the circle is biased).
So the best way to do this is to keep the viewport and window in the same width to prevent distortion .
int side = qmin (width (), height ()); int 2 ); int 2 );p Ainter.setviewport (x,y,side,side);
coordinate system of QT5