Qpixmap display image

Source: Internet
Author: User

Now we can show the image in the window and learn how to translate, zoom, rotate, and twist the image. Here we use the qpixmap class to display images.

1. Use qpixmap to display images.

1. Copy and back up the previous project folder. Here we will change the project folder to painter05. (As I have said before, it is a good habit to back up project directories frequently)

2. Create a folder in the debug folder of the project folder. I name it images here to store the images to use. I put a picture of linux.jpg here. As shown in.


 

3. Open the project in QT creator. (Open the. Pro file in the project folder ),.

4. Change the paintevent () function in the dialog. cpp file as follows.

Void dialog: paintevent (qpaintevent *)
{
Qpainter painter (this );
Qpixmap pix;
PIX. Load ("images/linux.jpg ");
Painter. drawpixmap (0, 0, 100,100, pix );
}

Create a qpixmap Class Object and add an image to it. Then, the image is displayed in a rectangle with a width and height of 100 at (0, 0. You can change the size of the rectangle to see the effect. The final program running effect is as follows.

(Note: The following operations are related to coordinates. perform operations first. We will explain the coordinate system in the next section .)

2. Change the coordinate origin to implement translation.

The translate () function in the qpainter class changes the coordinate origin. After the origin is changed, this point will become a new origin );

For example:

Void dialog: paintevent (qpaintevent *)
{
Qpainter painter (this );
Qpixmap pix;
PIX. Load ("images/linux.jpg ");
Painter. drawpixmap (0, 0, 100,100, pix );

Painter. Translate (100,100); // set (100,100) as the coordinate origin
Painter. drawpixmap (0, 0, 100,100, pix );
}

Here (100,100) is set to a new coordinate origin, so the following (0, 0) Point texture is equivalent to the previous (100,100) Point texture. The effect is as follows.

 

3. resize the image.

We can use the scaled () function in the qpixmap class to zoom in and out images.

For example:

Void dialog: paintevent (qpaintevent *)
{
Qpainter painter (this );
Qpixmap pix;
PIX. Load ("images/linux.jpg ");
Painter. drawpixmap (0, 0, 100,100, pix );

Qreal width = pix. Width (); // obtain the width and height of the previous image.
Qreal Height = pix. Height ();

PIX = pix. Scaled (width * 2, height * 2, QT: keepaspectratio );
// Increase the width and height of the image by two times and keep the width-to-height ratio within the given rectangle.
Painter. drawpixmap (100,100, pix );
}

The parameter QT: keepaspectratio is used for image scaling. We can view its help. Place the mouse pointer on the Code. When the F1 prompt appears, press the F1 key to view its help. Of course, we can also find the Code directly in the help.


This is an enumeration variable. Here there are three values. You can roughly understand it by looking at the image. QT: ignoreaspectratio does not maintain the aspect ratio of the image. QT :: keepaspectratio is to maintain the aspect ratio in the given rectangle, and the last is to maintain the aspect ratio, but may exceed the given rectangle. The rectangle given here is determined by the parameters given when we display the image, such as painter. drawpixmap (100,100, 100, pix); is in the rectangle where the width and height at () points are both.

The program running effect is as follows.


4. Rotating images.

The rotate () function of the qpainter class is used for rotation, which is centered on the origin by default. To change the center of the rotation, you can use the translate () function mentioned earlier.

For example:

Void dialog: paintevent (qpaintevent *)
{
Qpainter painter (this );
Qpixmap pix;
PIX. Load ("images/linux.jpg ");
Painter. Translate (50, 50); // Let the image center serve as the center of rotation
Painter. Rotate (90); // Rotate 90 degrees clockwise
Painter. Translate (-50,-50); // restore the origin
Painter. drawpixmap (0, 0, 100,100, pix );
}

Here, you must first change the rotation center, then rotate, and then restore the origin to achieve the desired effect.

Run the program as follows.

5. image distortion.

Image distortion is achieved using the qpainter class shear (qreal SH, qreal Sv) function. It has two parameters. The preceding parameters implement horizontal deformation and the subsequent parameters implement vertical deformation. When their values are 0, they are not distorted.

For example:

Void dialog: paintevent (qpaintevent *)
{
Qpainter painter (this );
Qpixmap pix;
PIX. Load ("images/linux.jpg ");
Painter. drawpixmap (0, 0, 100,100, pix );
Painter. shear (0.5, 0); // horizontal Distortion
Painter. drawpixmap (100,0, 100,100, pix );
}

The effect is as follows:


Other distortion effects:

Painter. shear (0, 0.5); // vertically distorted

Painter. shear (0.5, 0.5); // horizontal and vertical Distortion



 The changes in the image shape are actually realized by the changes in the coordinate system.

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.