QT学习(七)QT之重复绘图
1. 很多时候,需要在绘图设备上进行连续或者重复绘图,如果每次都要进行重新绘图,会大大降低绘图效率。比如,要连续绘制椭圆形,采用以下方式:
voidWidget::paint(){
QPainterpainter;
QColorblack(0,0,0);
QColorlime(0,255,0);
QPenpen(black,1.5);
QPicturepicture_temp;
picture_temp.load("E:/softwareprogramme/painter_QT/painter/drawing.pic");
painter.begin(&picture);
painter.setBrush(lime);
painter.setPen(pen);
painter.setRenderHint(QPainter::Antialiasing,true);
painter.drawPicture(0,0,picture_temp);
painter.drawEllipse(yvec,yvec,20,20);
yvec=yvec+15;
painter.end();
picture.save("E:/softwareprogramme/painter_QT/painter/drawing.pic");
imageArea->show();
}
(QPicture绘图说明:为了实现在QPicture上绘图并且显示,需要为其指定一个父widget来承载和显示其内容,可以选择QLabel,同时将QLabel加入QScrollArea中以便当图像过大时,可以通过滚动条来全部显示。
QPicturepicture;
imageLabel=newQLabel(this);
imageLabel->setPicture(picture);
imageArea->setWidget(imageLabel);
)
建立一个QPicture,在这上面进行绘图,然后将之保存,下一次再下载,绘制到一个QPicture上,然后再绘制椭圆形。这样每次都需要不断下载QPicture并且重新绘制,大大降低了绘制效率。随着QPicture上图像越来越大,速度会越来越低。
1. 利用双缓冲技术,将窗口部件存储为一个像素映射。再接到绘图事件,则可以在此像素映射上继续绘图,然后将像素映射复制到窗口部件中。实际上就是将原来图像进行了保存,然后可以进行重复绘制。QPixmap是实现双缓冲的QT类,QPixmap提供了大量函数可以使用,包括load、save等用于读取和存储各种图像类型;size、width、height可以获得其尺寸信息;depth获取图像位深;fill填充背景颜色等;
2. 现在对上边绘图进行重新实现:
pixmap=QPixmap(imageLabel->width(),imageLabel->height());
pixmap.fill(Qt::white);
imageLabel->setPixmap(pixmap);
QPainterpainter;
QPixmappixmap_temp(imageLabel->width(),imageLabel->height());
pixmap_temp=pixmap;
painter.begin(&pixmap_temp);
painter.setBrush(lime);
painter.setPen(pen);
painter.setRenderHint(QPainter::Antialiasing,true);
painter.drawEllipse(yvec,yvec,20,20);
painter.end();
yvec=yvec+15;
QPainterpainter1;
painter1.begin(&pixmap);
painter1.drawPixmap(0,0,pixmap_temp);
painter1.end();
imageLabel->setPixmap(pixmap);
imageArea->show();