1. 程式人生 > >QT學習(七)QT之重複繪圖

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();