QT三大繪圖類:Qpixmap/QImage/Qpicture

  • QPixmap
  • QImage
  • QPicture

以上都是QPaintDevice的子類


QPixmap

依賴硬件、加速顯示、適合小圖片

QPixmap的設計本來就是用來加速顯示,用paint繪圖時用QPixmap會比其他類的效果好很多。一般小圖片用QPixmap。

QImage

依賴軟件,直接像素訪問,適合大圖片

既然依賴軟件,那麼就不需要用硬件GUI的線程了,可以開個軟件的線程,可提高用戶UI體驗。


以上QPixmap和QImage如果要輸出圖片有兩種方式:

QImage image;
image.load(":/pics/earth.png");

QPainter painter(this);
painter.drawImage(0,0,image);
  • 1
  • 2
  • 3
  • 4
  • 5

當圖片較大時,可以用QImage先把圖片加載進來—調整大小—轉成QPixmap輸出:

QImage image;
image.load(":/pics/earth.png");

QPixmap pixmap = QPixmap::fromImage(image.scaled(size(),Qt::KeepAspectRatio));

QPainter painter(this);
painter.drawPixmap(0,0,pixmap);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

QPicture

記錄和回載QPainter的繪圖指令

使用 begin() 開始在QPicture上繪圖 
使用 end() 結束在QPicture上繪圖 
使用 save() 保存操作爲.pic文件 
使用 load() 加載.pic文件

舉個例子:

//save保存
QPicture picture;
QPainter painter;
painter.begin(&picture);
painter.drawRect(10,10,100,100);
painter.end();
picture.save("draw_record.pic");

//load加載
QPicture picture;
QPainter painter;
picture.load("draw_record.pic");
painter.begin(this);
painter.drawPicture(0,0,picture);
painter.end();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章