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