QT can display the basic image type, using Qimage, Qpxmap class can realize the display of the image

Source: Internet
Author: User

QT can display the basic image type, using Qimage, Qpxmap class can realize the display of the image, and use the method in the class to achieve the basic operation of the image (zoom, rotation).

1. QT can display the type of image

Refer to Qt's help documentation for the types of support that can be directly read and displayed in formats such as BMP, GIF, JPG, JPEG, PNG, TIFF, PBM, PGM, PPM, XBM, XPM.

2. How to display images in QT

The image is usually displayed with Qlabel, and the Qlabel class has a setpixmap () function that can be used to display the image. You can also draw images directly with Qpainter.

If the image is too large, directly with the Qlabel display, there will be some of the image is not displayed, then you can use the scroll area parts. Method: The Qlabel part is placed in the scroll area part, the two are in grid layout, when the image exceeds the size of the Qlabel part, it will produce a sliding bar, so that the image can be seen.

3. Method of Display

Step: First open an image, load the image file into the Qimage object, then use the Qpixmap object to obtain the image, and finally select a Qpixmap image object display with Qlabel.

The code is as follows:

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

    qstring filename;    filename=qfiledialog::getopenfilename (,                                             TR (),                                            ,                                             TR ());     (Filename.isempty ())     {          ;    }        {         QImage* img= QImage;         (!  (  img->load (filename)  )  )          {             qmessagebox::information (,                                       TR (),                                       TR ());             delete img;             ;        }         ui->label->setpixmap (Qpixmap::fromimage (*img));     }

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>



Note: You can also load an image directly with an object of the Qpixmap class using the Load function. Here's what the Qimage and Qpixmap classes are used for: Qimage is optimized for pixel-level access to images, Qpixmap is drawn using the drawing system of the underlying platform, does not provide pixel-level operations, and Qimage uses a hardware-independent drawing system.

4. Image Scaling

Image scaling takes the scaled function. Function prototypes

Qimage qimage::scaled (const qsize & size,qt::aspectratiomode Aspectratiomode = Qt::ignoreaspectratio, qt::transformationmodetransformmode = qt::fasttransformation) const

Here's how to use it, or use the img above:

qimage* imgscaled = qimage;*imgscaled=img->scaled (width, height, Qt::keepa Spectratio); Ui->label->setpixmap (Qpixmap::fromimage (*imgscaled));

The width and height of the scaled function represent the width and height of the scaled image, and the original image is scaled to (width,height) size.

5. Image rotation

Image rotation can use the transformed function of the Qimage class to pass in the Qmatrix object to the transformed function, and the Qmatrix object specifies the angle of rotation.

The code is as follows:

qimage* imgratate = qimage; Qmatrix matrix;matrix.rotate (); *imgrotate = img->transformed (matrix); Ui->label->setpixmap (QPixmap:: FromImage (*imgrotate));

Note: The parameter in the Rotate function is the angle of rotation, rotation is rotated clockwise, 270 degrees clockwise, that is, 90 degrees counterclockwise rotation.

6. Continuous image Scaling
With the image scaling basis, you can achieve continuous scaling of the image, you can place a horizontal slider (Chinese interpretation is not standard, that is, horizontal slider parts), sliding the position of the pulley to achieve continuous image scaling.

The horizontal slider part points to an integer value, which is the scaled image

Img->scaled (orignalwidth*value/100,
ORIGNALHEIGHT*VALUE/100,
Qt::keepaspectratio);

Orignalwidth and Orignalheight are the width and height of the original image.

Note: When scaling the original image in multiples, you only need to call the SetValue function of the horizontal slider part object within the corresponding slot function, because horizontal The slider part slider points to a value that, once changed, triggers the corresponding slot function to scale the image.


QT can display the basic image type, using Qimage, Qpxmap class can realize the display of the image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.