• Qt学习10:像丝一样滑(双缓冲)


     
    void CannonField::paintEvent(QPaintEvent *e)
    {
    	// -------------------------------------
    	// QPaintEvent包含一个必须被刷新的窗口部件的区域
        // QPainter默认只能在paintEvent里面调用
    	// -------------------------------------
    	QRect cr = cannonRect();
    
    	// --------------------------------------
    	// 只要有一个象素相交,就返回true。
    	// --------------------------------------
    	if (!e->rect().intersects(cr))
    	{
            return;
    	}
    
    	QPixmap pix(cr.size());
    	//用this的背景色填充The QPoint offset 
    	//defines a point in widget coordinates 
    	//to which the pixmap's top-left pixel will be mapped to.
    	pix.fill(this, cr.topLeft());
    
    	//先绘到QPixmap上,再把QPixmap贴到this.
    	QPainter p(&pix);
    
    	//图片设为蓝色
    	p.setBrush(Qt::blue);
    	p.setPen(Qt::PenStyle::NoPen);
    
    	//这个移的是坐标系,相当于QRect在做所有的运算时,
    	//都先要offerset(-0, -pix.height()   
    	p.translate(0, pix.height());
    
    	p.drawPie(QRect(-35, -35, 70, 70), 0, 90*16);
    
    	//rotate是旋轩坐标系.顺时针方向为正,逆时针为负.
    	p.rotate(-ang);
    	p.drawRect(QRect(33, -8, 15, 8));
    	//Ends painting. Any resources used while painting are released. 
    	p.end();
    
    	//only one painter at a time
    	p.begin(this);
    	p.drawPixmap(cr.topLeft(), pix);
    }
    

    右下角绘制:

    void CannonField::paintEvent(QPaintEvent *e)
    {
    	// -------------------------------------
    	// QPaintEvent包含一个必须被刷新的窗口部件的区域
        // QPainter默认只能在paintEvent里面调用
    	// -------------------------------------
    	QRect cr = cannonRect();
    
    	// --------------------------------------
    	// 只要有一个象素相交,就返回true。
    	// --------------------------------------
    	if (!e->rect().intersects(cr))
    	{
            return;
    	}
    
    	QPixmap pix(cr.size());
    	//用this的背景色填充The QPoint offset 
    	//defines a point in widget coordinates 
    	//to which the pixmap's top-left pixel will be mapped to.
    	pix.fill(this, cr.topLeft());
    
    	//先绘到QPixmap上,再把QPixmap贴到this.
    	QPainter p(&pix);
    
    	//图片设为蓝色
    	p.setBrush(Qt::blue);
    	p.setPen(Qt::PenStyle::NoPen);
    
    	//这个移的是坐标系,相当于QRect在做所有的运算时,
    	//都先要offerset(-0, -pix.height()   
    	//p.translate(0, pix.height());
    
    	//p.drawPie(QRect(-35, -35, 70, 70), 0, 90*16);
    	p.translate(pix.width(), pix.height());
    	p.drawPie(QRect(-35, -35, 70, 70), 90*16, 90*16);
    	//p.drawRect(QRect(-35, -35, 70, 70));
    
    	//rotate是旋轩坐标系.顺时针方向为正,逆时针为负.
    	p.rotate(ang);
    	p.drawRect(QRect(-33-15, -8, 15, 8));
    	//Ends painting. Any resources used while painting are released. 
    	p.end();
    
    	//only one painter at a time
    	p.begin(this);
    	p.drawPixmap(cr.topLeft(), pix);
    }



    1.先画到临时的pix上,再一次绘出来,双缓冲

    2.drawPie的起始角度不要忘了*16,还有它是以逆时针旋转算角度的

  • 相关阅读:
    error=Error Domain=NSURLErrorDomain Code=-1003
    修改mac host
    页面之间的跳转效果
    iOS开发200个tips总结(一)
    2020Python练习七——文件处理2
    2020python练习六——文件处理1
    【2020Python错题本】week1错误集
    【2020Python修炼记14】Python语法入门—文件处理
    【2020Python修炼记13】Python语法入门—字符编码
    2013年省赛I题 Thrall’s Dream
  • 原文地址:https://www.cnblogs.com/hgy413/p/3693565.html
Copyright © 2020-2023  润新知