QPixmap繼承了QPaintDevice,所以,你可使用QPainter直接在上面繪製圖形。QPixmap也能夠接受一個字符串做爲一個文件的路徑來顯示這個文件,好比你想在程序之中打開png、jpeg之類的文件,就可使用QPixmap。使用QPainter的drawPixmap()函數能夠把這個文件繪製到一個QLabel、QPushButton或者其餘的設備上面。QPixmap是針對屏幕進行特殊優化的,所以,它與實際的底層顯示設備息息相關。注意,這裏說的顯示設備並非硬件,而是操做系統提供的原生的繪圖引擎。因此,在不一樣的操做系統平臺下,QPixmap的顯示可能會有所差異。
QPixmap提供了靜態的grabWidget()和grabWindow()函數,用於將自身圖像繪製到目標上。同時,在使用QPixmap時,你能夠直接使用傳值也不須要傳指針,由於QPixmap提供了「隱式數據共享」。關於這一點,咱們會在之後的章節中詳細描述,這裏只要知道傳遞QPixmap沒必要須使用指針就行了。
QBitmap繼承自QPixmap,所以具備QPixmap的全部特性。QBitmap的色深始終爲1. 色深這個概念來自計算機圖形學,是指用於表現顏色的二進制的位數。咱們知道,計算機裏面的數據都是使用二進制表示的。爲了表示一種顏色,咱們也會使用二進制。好比咱們要表示8種顏色,須要用3個二進制位,這時咱們就說色深是3. 所以,所謂色深爲1,也就是使用1個二進制位表示顏色。1個位只有兩種狀態:0和1,所以它所表示的顏色就有兩種,黑和白。因此說,QBitmap其實是隻有黑白兩色的圖像數據。
因爲QBitmap色深小,所以只佔用不多的存儲空間,因此適合作光標文件和筆刷。
下面咱們來看同一個圖像文件在QPixmap和QBitmap下的不一樣表現:
void PaintedWidget::paintEvent(QPaintEvent *
event)
![](http://static.javashuo.com/static/loading.gif)
{
![](http://static.javashuo.com/static/loading.gif)
QPainter painter(
this);
![](http://static.javashuo.com/static/loading.gif)
QPixmap pixmap(
"Cat.png");
![](http://static.javashuo.com/static/loading.gif)
QBitmap bitmap(
"Cat.png");
![](http://static.javashuo.com/static/loading.gif)
painter.drawPixmap(10, 10, 128, 128, pixmap);
![](http://static.javashuo.com/static/loading.gif)
painter.drawPixmap(140, 10, 128, 128, bitmap);
![](http://static.javashuo.com/static/loading.gif)
QPixmap pixmap2(
"Cat2.png");
![](http://static.javashuo.com/static/loading.gif)
QBitmap bitmap2(
"Cat2.png");
![](http://static.javashuo.com/static/loading.gif)
painter.drawPixmap(10, 140, 128, 128, pixmap2);
![](http://static.javashuo.com/static/loading.gif)
painter.drawPixmap(140, 140, 128, 128, bitmap2);
![](http://static.javashuo.com/static/loading.gif)
}
先來看一下運行結果:
這裏咱們給出了兩張png圖片。Cat.png是沒有透明色的純白背景,而Cat2.png是具備透明色的背景。咱們分別使用QPixmap和QBitmap來加載它們。注意看它們的區別:白色的背景在Qbitmap中消失了,而透明色在QBitmap中轉換成了黑色;其餘顏色則是使用點的疏密程度來體現的。
QPixmap使用底層平臺的繪製系統進行繪製,沒法提供像素級別的操做,而QImage則是使用獨立於硬件的繪製系統,其實是本身繪製本身,所以提供了像素級別的操做,而且可以在不一樣系統之上提供一個一致的顯示形式。
如上圖所示(出自Qt API文檔),咱們聲明瞭一個QImage對象,大小是3 x 3,顏色模式是RGB32,即便用32位數值表示一個顏色的RGB值,也就是說每種顏色使用8位。而後咱們對每一個像素進行顏色賦值,從而構成了這個圖像。你能夠把QImage想象成一個RGB顏色的二維數組,記錄了每一像素的顏色。
最後一個須要說明的是QPicture。這是一個能夠記錄和重現QPainter命令的繪圖設備。QPicture將QPainter的命令序列化到一個IO設備,保存爲一個平臺獨立的文件格式。這種格式有時候會是「元文件(meta-files)」。Qt的這種格式是二進制的,不一樣於某些本地的元文件,Qt的pictures文件沒有內容上的限制,只要是可以被QPainter繪製的元素,不管是字體仍是pixmap,或者是變換,均可以保存進一個picture中。
QPicture是平臺無關的,所以它可使用在多種設備之上,好比svg、pdf、ps、打印機或者屏幕。回憶下咱們這裏所說的QPaintDevice,其實是說能夠有QPainter繪製的對象。QPicture使用系統的分辨率,而且能夠調整QPainter來消除不一樣設備之間的顯示差別。
若是咱們要記錄下QPainter的命令,首先要使用QPainter::begin()函數,將QPicture實例做爲參數傳遞進去,以便告訴系統開始記錄,記錄完畢後使用QPainter::end()命令終止。代碼示例以下:
![](http://static.javashuo.com/static/loading.gif)
QPicture picture;
![](http://static.javashuo.com/static/loading.gif)
QPainter painter;
![](http://static.javashuo.com/static/loading.gif)
painter.begin(&picture);
// paint in picture
![](http://static.javashuo.com/static/loading.gif)
painter.drawEllipse(10,20, 80,70);
// draw an ellipse
![](http://static.javashuo.com/static/loading.gif)
painter.end();
// painting done
![](http://static.javashuo.com/static/loading.gif)
picture.save(
"drawing.pic");
// save picture
若是咱們要重現命令,首先要使用QPicture::load()函數進行裝載:
![](http://static.javashuo.com/static/loading.gif)
QPicture picture;
![](http://static.javashuo.com/static/loading.gif)
picture.load(
"drawing.pic");
// load picture
![](http://static.javashuo.com/static/loading.gif)
QPainter painter;
![](http://static.javashuo.com/static/loading.gif)
painter.begin(&myImage);
// paint in myImage
![](http://static.javashuo.com/static/loading.gif)
painter.drawPicture(0, 0, picture);
// draw the picture at (0,0)
![](http://static.javashuo.com/static/loading.gif)
painter.end();