• 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,还有它是以逆时针旋转算角度的

  • 相关阅读:
    Merlin 的魔力: SpringLayout 管理器
    setOpaque(true);设置控件不透明
    运用 BoxLayout 进行 Swing 控件布局
    李洪强iOS开发本人集成环信的经验总结_02_基本配置
    李洪强iOS开发之-PCH文件的配置
    李洪强iOS开发本人集成环信的经验总结_01环信SDK的导入
    iOS开发UI篇—Quartz2D使用(矩阵操作)
    iOS开发UI篇—Quartz2D使用(图形上下文栈)
    iOS开发UI篇—Quartz2D简单使用(二)
    iOS开发UI篇—Quartz2D简单使用(一)
  • 原文地址:https://www.cnblogs.com/hgy413/p/3693565.html
Copyright © 2020-2023  润新知