QT Learning Pathway (29): Drawing equipment

Source: Internet
Author: User

A drawing device is a subclass of an inherited Qpainterdevice. QT provides a total of four such classes, namely Qpixmap, Qbitmap, Qimage and Qpicture. Qpixmap specifically optimizes the display of the image on the screen, and Qbitmap is a subclass of Qpixmap, whose color depth is limited to 1, you can use the Qpixmap isqbitmap () function to determine if this qpixmap is a qbitmap. Qimage is specifically optimized for pixel-level access to images. Qpicture can record and reproduce the Qpainter commands. Below we will introduce these four kinds of drawing equipment in two parts.

Qpixmap inherits Qpaintdevice, so you can use Qpainter to draw graphs directly above. Qpixmap can also accept a string as a path to a file to display the file, such as you want to open in the program PNG, JPEG and other files, you can use the Qpixmap. Use the Qpainter Drawpixmap () function to draw this file to a Qlabel, Qpushbutton, or other device. Qpixmap is specially optimized for the screen, so it is closely related to the actual underlying display device. Notice that the display device here is not hardware, but the native drawing engine provided by the operating system. So, under the different operating system platform, the Qpixmap display may have the difference.

Qpixmap provides a static Grabwidget () and Grabwindow () function that draws its own image to the target. At the same time, when using Qpixmap, you can use the values directly and do not need to pass the pointer, because QPIXMAP provides "implicit data sharing." In this regard, we will describe in detail in a later chapter, as long as you know that passing qpixmap do not have to use pointers.

Qbitmap inherits from Qpixmap, so it has all the attributes of Qpixmap. The Qbitmap color depth is always 1. The concept of color depth is derived from computer graphics, which refers to the number of digits used to represent the color of the binary. We know that the data inside the computer is represented by the binary system. In order to represent a color, we also use binary. For example, we want to say 8 colors, need to use 3 bits, then we say color depth is 3. Therefore, the so-called color deep 1, that is, the use of 1 bits to represent color. 1 bits have only two states: 0 and 1, so there are two kinds of colors, black and white. So, Qbitmap is actually only black-and-white image data.

Because the Qbitmap color is deep and small, it takes up very little storage space, so it is suitable for cursor files and brushes.

Here we look at the different manifestations of the same image file under Qpixmap and Qbitmap:

void PaintedWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPixmap pixmap("Cat.png");
QBitmap bitmap("Cat.png");
painter.drawPixmap(10, 10, 128, 128, pixmap);
painter.drawPixmap(140, 10, 128, 128, bitmap);
QPixmap pixmap2("Cat2.png");
QBitmap bitmap2("Cat2.png");
painter.drawPixmap(10, 140, 128, 128, pixmap2);
painter.drawPixmap(140, 140, 128, 128, bitmap2);
}

Let's take a look at the results of the operation:

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.