Qt 圖像類 QImage QPixmap QBitmap QPicture 之區別與用途

 自己整理的,備忘錄,參考Qt自帶幫助:

qthelp://com.trolltech.qt.486/qdoc/qpixmap.html#details


Qt provides four classes for handlingimage data:QImage, Pixmap, QBitmapandQPicture.

繼承關係:

QPaintDevice->QPixmap->QBitmap

 QPaintDevice->QImage

 QPaintDevice->QPicture

 

QPixmap:   is an off-screen image representation thatcan be used as a paint device

QBitmap:   provides monochrome (1-bit depth) pixmaps

QImage :   provides a hardware-independent imagerepresentation that allows direct access to the pixel data, and can be used asa paint device

QPicture:is a paint device that records and replays QPainter commands. 

 

QImage is designed and optimizedfor I/O, and for direct pixel access and manipulation,

QPixmap is designed and optimizedfor showing images on screen.

QBitmap is only a convenienceclass that inherits QPixmap, ensuring a depth of 1. The isQBitmap() functionreturns true if a QPixmap object is really a bitmap, otherwise returns false.

QPicture class is a paint devicethat records and replays QPainter commands.

 ------------------------------------QPixmap 說明-------------------------------------------------------

A QPixmapcan easily be displayed onthe screen using QLabel or one of QAbstractButton's subclasses (suchasQPushButton and QToolButton). QLabel has a pixmap property, whereasQAbstractButton has an icon property.

In addition to the ordinaryconstructors, a QPixmap can be constructed using the static grabWidget() andgrabWindow() functions which creates a QPixmap and paints the given widget, orwindow, into it.

QPixmap objects can be passed aroundby value since the QPixmap class uses implicit data sharing. For moreinformation, see the Implicit Data Sharing documentation. QPixmap objects canalso be streamed.

Note that the pixel data in a pixmapis internal and is managed by the underlying window system. Because QPixmap isaQPaintDevice subclass, QPainter can be used to draw directly onto pixmaps.Pixels can only be accessed through QPainterfunctions or by converting theQPixmap to a QImage. However, the fill() function is available for initializingthe entire pixmap with a given color.

There are functions to convert betweenQImage and QPixmap. Typically, the QImage class is used to load an image file,optionally manipulating the image data, before the QImage object is convertedinto a QPixmap to be shown on screen. Alternatively, if no manipulation isdesired, the image file can be loaded directly into a QPixmap. On Windows, theQPixmap class also supports conversion between HBITMAP and QPixmap. On Symbian,the QPixmap class also supports conversion between CFbsBitmap and QPixmap.

 ---------------------------------QPicture說明--------------------------------------------------------------------------------

 

//QPicture例子:
         QPicture picture;
        QPainter painter;
         painter.begin(&picture);           // paint in picture
         painter.drawEllipse(10,20, 80,70); //draw an ellipse
         painter.end();                     // painting done
        picture.save("drawing.pic");       //save picture
 
         QPicture picture;
        picture.load("drawing.pic");           // load picture
         QPainter painter;
         painter.begin(&myImage);               // paint in myImage
         painter.drawPicture(0, 0,picture);    // draw the picture at (0,0)
         painter.end();                         // painting done


---------------------------------QImage說明-------------------------------------------------------------------------------------------


QImage讀寫  圖像文件:

QImage provides several ways of loading an image file: 

The file can be loaded when constructing the QImage object, or by using theload()orloadFromData()functions later on. QImage also provides the staticfromData()function, constructing a QImage from the given data. When loading an image, the file name can either refer to an actual file on disk or to one of the application's embedded resources. See The Qt Resource System overview for details on how to embed images and other resource files in the application's executable.
Simply call the save() function to save a QImage object.


 QImage image(3, 3, QImage::Format_RGB32);
 QRgb value;

 value = qRgb(189, 149, 39); // 0xffbd9527
 image.setPixel(1, 1, value);

 value = qRgb(122, 163, 39); // 0xff7aa327
 image.setPixel(0, 1, value);
 image.setPixel(1, 0, value);

 value = qRgb(237, 187, 51); // 0xffedba31
 image.setPixel(2, 1, value);



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章