我知道如何painter.setWindow
运作。例如,如果我将小部件最大化,那么我在该小部件中绘制的任何内容都会以相同的比例变大。
但我不明白到底painter.setViewport
是做什么的。谁能向我解释它是如何工作的并给我一个例子?
As the documentation writes, it is the device coordinate system, and not the logical. They are not necessarily equal to each other even though the default is the same.
This functionality mostly presents because of the API compatibility. That was necessary at the Qt 3 days before the real transformation feature support.
You can do everything with translate and scale what you can also do with viewports. It is just personal preference, albeit the former is more inline with the Vector API in Qt.
我想通了,这就是答案,如果有人用谷歌搜索它:
我编写了这段代码来查看差异以及两者的工作方式,我在调整窗口大小时输出window
和坐标。viewport
(只是使用了任意数字)
QPen pen(Qt::blue,3,Qt::SolidLine);
painter.setPen(pen);
painter.setViewport(50,50,100,100);
painter.setWindow(-100,-150,200,200);
QRect rect= painter.viewport();
QRect wind= painter.window();
cout<< rect.x() << " "<< rect.y() << " "<< rect.height() << " "<< rect.width() <<endl;
cout<< wind.x() << " "<< wind.y() << " "<< wind.height() << " "<< wind.width() <<endl;
painter.drawRect(0,0,200,202);
并用这两个注释中的任何一行运行程序
painter.setViewport(50,50,100,100);
painter.setWindow(-100,-150,200,200);
在评论setwindow
和设置setviewport
时,矩形应该变小,为什么?
在调整窗口大小时,逻辑(窗口)和物理(视口)坐标都应该变化相同。但这里我是set
Viewport
不变的,所以逻辑坐标(绘图之一)只是一个变化。因此,当窗口调整得更大时,逻辑坐标必须适合小常数viewport
,因此矩形变得更小