QT Coordinate System Insights

Source: Internet
Author: User

The window coordinates are logical coordinates and are based on the viewport coordinate system.

Viewport coordinates are physical coordinates and are based on the drawing device coordinate system.

window coordinates are always mapped to the final target in viewport coordinates :

Qpainter::setwindow modified the window position and size (the upper left corner redefined a value and length)

Qpainter::setviewport changes the viewport position and number of pixels (the upper-left corner moves to the corresponding position and number of pixels)

--------------------------------------------------------------------------------------------------------------- ---------------------------------

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 and are based on the viewport coordinate system. viewport coordinates are physical coordinates and are 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 this sentence will be said next to the viewport), 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: The x-coordinate of the top left corner of the window
Y: y-coordinate of the upper-left corner of the window
Width: Window length (not pixels)
Height: Window height (not pixels)

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 (pixels)
Height: Set viewport width (pixels)

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 wolf 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 value is 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 the window size is a unit length of 100/ 400=0.25 pixels, so when you draw a 100x100 unit-length rectangle, the actual large hour 25x25 pixels, so it becomes 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).

Finally, let's look at an example of a window and a viewport set together.
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);}

This sets the window coordinates ( -50,-50) map to the origin of the viewport, Mapping the window 100x100 unit length to the 100x100 pixel size of the viewport
The window's logical coordinate -50,-50 is the coordinates of the viewport (0,0), which is the 50, 50 coordinates of the drawing device, so the window coordinate (0,0) position is the coordinate of the drawing device (100,100).
Because the window coordinates 100 units of length map to 100 pixels of viewport coordinates, the painter.drawRect(0,0, 50, 50); result of one sentence is to draw a 50x50 pixel rectangle at the (100,100) position of the drawing device.

Summary:

To get the real position of the Qpainter drawing, go through two steps

The first step:
The window coordinates are converted to viewport coordinates, and the conversion formula is:
vp_width / win_width * (draw_x - win_x)
Where Vp_width is the viewport length, win_width is the window length, draw_x is the actual x-left coordinate to be drawn, win_x is the upper-left coordinate of the window, and the y-coordinate is the same

The window size is converted to the viewport size, and the conversion formula is:
draw_width * vp_width / win_width
Draw_width is the length of the window to be drawn, Vp_width is the viewport length, win_width is the window length

Height similarly

Step Two:

Viewport coordinates are converted to drawing device coordinates, and this step simply adds up.
The actual coordinates x= the viewport coordinates of the previous step conversion +vp_x
Vp_x is the upper-left x-coordinate of the viewport, and the actual coordinate Y

Reprint Address: http://blog.csdn.net/syzobelix/article/details/9377863

QT Coordinate System Insights

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.