3

I know how painter.setWindow works. for example, if I maximize the widget larger, whatever I drew in that widget gets larger also in the same ratio.

but I can't understand what painter.setViewport exactly do. Can anyone explain to me how it works and give me an example?

László Papp
  • 51,870
  • 39
  • 111
  • 135
Tito Tito
  • 248
  • 3
  • 10

2 Answers2

3

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.

László Papp
  • 51,870
  • 39
  • 111
  • 135
2

I figured it out and here's the answer, If someone googled it:

I made this code to see the difference and how both work, I'm outputting the window and viewport coordinates while resizing the window. (just used arbitrary numbers)

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);

and ran the program with either line of those two commented

painter.setViewport(50,50,100,100);
painter.setWindow(-100,-150,200,200);

While commenting setwindow, and setting setviewport, the rectangle should get smaller,why?

On resizing the window, the logical(window) and the physical(viewport) coordinates should both change the same.But here I set Viewport constant, so the logical coordinates(the drawing one) is only one changes. So while the window resize larger the logical coordinates has to fit into the small constant viewport and consequently the rectangle get smaller

Tito Tito
  • 248
  • 3
  • 10