Qt二维绘图基础之setView和setWindow

Qt绘图之setView

该设置是针对绘图设备的坐标,假设定义paintEvent如下所示
void Widget::paintEvent(QPaintEvent *)
{
    QPainter  paint(this);
    paint.setRenderHint(QPainter::Antialiasing);   //设置绘图返走样
    paint.setRenderHint(QPainter::TextAntialiasing);//设置字体反走样
    int side = qMin(this->width(),this->height());
    paint.setPen(QColor("#521258"));
    paint.drawLine(QLineF(QPointF(0,0),QPointF(width(),height())));
    paint.drawLine(QLineF(QPointF(0,height()),QPointF(width(),0)));
}

编译之后输出如下所示
Qt二维绘图基础之setView和setWindow_第1张图片此时绘制的图像符合我们的预期,下边开始设置setView

void Widget::paintEvent(QPaintEvent *)
{
    QPainter  paint(this);
    paint.setRenderHint(QPainter::Antialiasing);        //设置绘图返走样
    paint.setRenderHint(QPainter::TextAntialiasing);    //设置字体反走样
    paint.setViewport(width()/2,height()/2,200,200);
    int side = qMin(this->width(),this->height());
    paint.setPen(QColor("#521258"));
    paint.drawLine(QLineF(QPointF(0,0),QPointF(width(),height())));
    paint.drawLine(QLineF(QPointF(0,height()),QPointF(width(),0)));
    paint.setPen(QColor("#A02565"));
    }

Qt二维绘图基础之setView和setWindow_第2张图片可以这样理解,setView是重定向绘图,但同时View设置的大小也会影响绘图整体的大小

下边开始同时设置setWindow和setView

QPainter  paint(this);
    paint.setRenderHint(QPainter::Antialiasing);        //设置绘图返走样
    paint.setRenderHint(QPainter::TextAntialiasing);    //设置字体反走样
    int side = qMin(this->width(),this->height());
    paint.setPen(QColor("#521258"));
    paint.drawLine(QLineF(QPointF(0,0),QPointF(width(),height())));
    paint.drawLine(QLineF(QPointF(0,height()),QPointF(width(),0)));
    paint.setPen(QColor("#A02565"));
    paint.setViewport(width()/2,height()/2,200,200);
    paint.setWindow(0,0,side,side);
    for(int i = 0; i < 400; i++)
    {
        paint.drawEllipse(QPointF(0,i),80,80);
        qDebug()<<"Start to paint and rotate~"<<endl;
        paint.rotate(18);
    }

显示如图所示,
Qt二维绘图基础之setView和setWindow_第3张图片可以看到在窗体的中间画了一幅图,但是当我们窗口放置最大化时,绘制的图形也是在View之内,但是坐标执行的是window的坐标,且View的左上角是window的坐标原点,绘制是基于Window来绘制的。

初始情况下,默认view和window是重合的,是窗口的左上角顶点。
如有问题,可留言提问,该文章针对想要学习自绘控件的初学者,高手越过,如有错误,请指正。

你可能感兴趣的:(qt,qt基础类的使用)