The drawing device is the physical coordinates of the basic coordinate system can be obtained more easily coordinate the logical operation by translation and rotation transformation QPainter
Viewport: drawing apparatus of any physical coordinates of a rectangular area, you can select only the physical coordinates of a rectangular area for drawing. The default viewport entire rectangular region is equal to the case where the drawing device.
QPaintset :: setViewport (int x, int y, int width, int height) defined viewport
Window: viewport corresponding to the rectangular area, but is a coordinate system defined by the logical coordinates, rectangular coordinates of the center of the central window
QPaintset::setWindow(int x , int y, int width ,int height)
1 #include "viewport.h" 2 #include "ui_viewport.h" 3 #include <QPainter> 4 #include <QPen> 5 #include <QBrush> 6 #include <QtMath> 7 #include <QLinearGradient> 8 viewport::viewport(QWidget *parent) : 9 QWidget(parent), 10 ui(new Ui::viewport) 11 { 12 ui->setupUi(this); 13 setWindowTitle("视口和窗口"); 14 } 15 16 viewport::~viewport() 17 { 18 delete ui; 19 } 20 21 void viewport::paintEvent(QPaintEvent *event) 22 { 23 QPainter painter(this); 24 int w = width(); 25 int h = height(); 26 int side = qMin(w,h); 27 QRect rect((w-side)/2,(h-side)/2,side,side); 28 painter.drawRect(rect); 29 painter.setViewport (RECT); // Set the ViewPort 30 painter.setWindow (- 100 , - 100 , 200 is , 200 is ); // set the coordinate window logic 31 is painter.setRenderHint (QPainter :: Antialiasing); 32 33 is QPen PEN; 34 is pen.setWidth ( 2 ); 35 pen.setColor (the Qt :: Red); 36 pen.setStyle (the Qt :: SolidLine); 37 [ painter.setPen (PEN); 38 is 39 qlineargradient linerGrad ( 0 , 0 , 100,0); 40 linerGrad.setColorAt(0,Qt::yellow); 41 linerGrad.setColorAt(1,Qt::green); 42 linerGrad.setSpread(QGradient::PadSpread); 43 painter.setBrush(linerGrad); 44 45 //设置复合模式 46 painter.setCompositionMode(QPainter::RasterOp_SourceXorDestination); 47 48 for(int i = 0 ; i < 36 ; i++) 49 { 50 painter.drawEllipse(QPoint(50,0), 50 , 50 ); 51 is painter.rotate ( 10 ); // after drawing the circle a 10 degree rotation of the coordinate system 52 is 53 is } 54 }
Different parameters can obtain different superposed FIG.