Struct rute::auto::pixmap::Pixmap

source ·
pub struct Pixmap<'a> { /* private fields */ }
Expand description

Notice these docs are heavy WIP and not very relevent yet

Qt provides four classes for handling image data: QImage, QPixmap, QBitmap and QPicture. QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen. QBitmap is only a convenience class that inherits QPixmap, ensuring a depth of 1. The isQBitmap() function returns true if a QPixmap object is really a bitmap, otherwise returns false. Finally, the QPicture class is a paint device that records and replays QPainter commands.

A QPixmap can easily be displayed on the screen using QLabel or one of QAbstractButton’s subclasses (such as QPushButton and QToolButton). QLabel has a pixmap property, whereas QAbstractButton has an icon property.

QPixmap objects can be passed around by value since the QPixmap class uses implicit data sharing. For more information, see the Implicit Data Sharing documentation. QPixmap objects can also be streamed.

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

There are functions to convert between QImage and QPixmap. Typically, the QImage class is used to load an image file, optionally manipulating the image data, before the QImage object is converted into a QPixmap to be shown on screen. Alternatively, if no manipulation is desired, the image file can be loaded directly into a QPixmap.

QPixmap provides a collection of functions that can be used to obtain a variety of information about the pixmap. In addition, there are several functions that enables transformation of the pixmap.

Reading and Writing Image Files

QPixmap provides several ways of reading an image file: The file can be loaded when constructing the QPixmap object, or by using the load() or loadFromData() functions later on. 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 QPixmap object.

The complete list of supported file formats are available through the QImageReader::supportedImageFormats() and QImageWriter::supportedImageFormats() functions. New file formats can be added as plugins. By default, Qt supports the following formats:

  • Format
  • Description
  • Qt’s support
  • BMP
  • Windows Bitmap
  • Read/write
  • GIF
  • Graphic Interchange Format (optional)
  • Read
  • JPG
  • Joint Photographic Experts Group
  • Read/write
  • JPEG
  • Joint Photographic Experts Group
  • Read/write
  • PNG
  • Portable Network Graphics
  • Read/write
  • PBM
  • Portable Bitmap
  • Read
  • PGM
  • Portable Graymap
  • Read
  • PPM
  • Portable Pixmap
  • Read/write
  • XBM
  • X11 Bitmap
  • Read/write
  • XPM
  • X11 Pixmap
  • Read/write

Pixmap Information

QPixmap provides a collection of functions that can be used to obtain a variety of information about the pixmap:

  • Available Functions

  • Geometry

  • The size(), width() and height() functions provide information about the pixmap’s size. The rect() function returns the image’s enclosing rectangle.

  • Alpha component

  • The hasAlphaChannel() returns true if the pixmap has a format that respects the alpha channel, otherwise returns false. The hasAlpha(), setMask() and mask() functions are legacy and should not be used. They are potentially very slow. The createHeuristicMask() function creates and returns a 1-bpp heuristic mask (i.e. a QBitmap) for this pixmap. It works by selecting a color from one of the corners and then chipping away pixels of that color, starting at all the edges. The createMaskFromColor() function creates and returns a mask (i.e. a QBitmap) for the pixmap based on a given color.

  • Low-level information

  • The depth() function returns the depth of the pixmap. The defaultDepth() function returns the default depth, i.e. the depth used by the application on the given screen. The cacheKey() function returns a number that uniquely identifies the contents of the QPixmap object. The x11Info() function returns information about the configuration of the X display used by the screen to which the pixmap currently belongs. The x11PictureHandle() function returns the X11 Picture handle of the pixmap for XRender support. Note that the two latter functions are only available on x11.

Pixmap Conversion

A QPixmap object can be converted into a QImage using the toImage() function. Likewise, a QImage can be converted into a QPixmap using the fromImage(). If this is too expensive an operation, you can use QBitmap::fromImage() instead.

To convert a QPixmap to and from HICON you can use the QtWinExtras functions QtWin::toHICON() and QtWin::fromHICON() respectively.

Pixmap Transformations

QPixmap supports a number of functions for creating a new pixmap that is a transformed version of the original:

The scaled(), scaledToWidth() and scaledToHeight() functions return scaled copies of the pixmap, while the copy() function creates a QPixmap that is a plain copy of the original one.

The transformed() function returns a copy of the pixmap that is transformed with the given transformation matrix and transformation mode: Internally, the transformation matrix is adjusted to compensate for unwanted translation, i.e. transformed() returns the smallest pixmap containing all transformed points of the original pixmap. The static trueMatrix() function returns the actual matrix used for transforming the pixmap.

Note: When using the native X11 graphics system, the pixmap becomes invalid when the QApplication instance is destroyed.

See also: Bitmap Image [ImageReader] [ImageWriter]

Licence

The documentation is an adoption of the original Qt Documentation and provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation.

Implementations

Swaps pixmap other with this pixmap. This operation is very fast and never fails.

Returns true if this is a null pixmap; otherwise returns false.

A null pixmap has zero width, zero height and no contents. You cannot draw in a null pixmap.

Returns the width of the pixmap.

See also: [size()] {QPixmap#Pixmap Information}{Pixmap Information}

Returns the height of the pixmap.

See also: [size()] {QPixmap#Pixmap Information}{Pixmap Information}

Returns the size of the pixmap.

See also: [width()] [height()] {QPixmap#Pixmap Information}{Pixmap Information}

Returns the pixmap’s enclosing rectangle.

See also: {QPixmap#Pixmap Information}{Pixmap Information}

Returns the depth of the pixmap.

The pixmap depth is also called bits per pixel (bpp) or bit planes of a pixmap. A null pixmap has depth 0.

See also: [default_depth()] {QPixmap#Pixmap Information}{Pixmap Information}

Returns the default pixmap depth used by the application.

On all platforms the depth of the primary screen will be returned.

Note: QGuiApplication must be created before calling this function.

See also: [depth()] [Colormap::depth] {QPixmap#Pixmap Information}{Pixmap Information}

Use QPainter or the fill(QColor) overload instead.

Use QPainter or the fill(QColor) overload instead.

Fills the pixmap with the given color.

The effect of this function is undefined when the pixmap is being painted on.

See also: {QPixmap#Pixmap Transformations}{Pixmap Transformations}

Use QPainter or the fill(QColor) overload instead.

Use QPainter or the fill(QColor) overload instead.

Fills the pixmap with the given color.

The effect of this function is undefined when the pixmap is being painted on.

See also: {QPixmap#Pixmap Transformations}{Pixmap Transformations}

Use QPainter or the fill(QColor) overload instead.

Use QPainter or the fill(QColor) overload instead.

Fills the pixmap with the given color.

The effect of this function is undefined when the pixmap is being painted on.

See also: {QPixmap#Pixmap Transformations}{Pixmap Transformations}

Extracts a bitmap mask from the pixmap’s alpha channel.

Warning: This is potentially an expensive operation. The mask of the pixmap is extracted dynamically from the pixeldata.

See also: [set_mask()] {QPixmap#Pixmap Information}{Pixmap Information}

Sets a mask bitmap.

This function merges the mask with the pixmap’s alpha channel. A pixel value of 1 on the mask means the pixmap’s pixel is unchanged; a value of 0 means the pixel is transparent. The mask must have the same size as this pixmap.

Setting a null mask resets the mask, leaving the previously transparent pixels black. The effect of this function is undefined when the pixmap is being painted on.

Warning: This is potentially an expensive operation.

See also: [mask()] {QPixmap#Pixmap Transformations}{Pixmap Transformations} Bitmap

Returns the device pixel ratio for the pixmap. This is the ratio between device pixels and device independent pixels .

Use this function when calculating layout geometry based on the pixmap size: QSize layoutSize = image.size() / image.devicePixelRatio()

The default value is 1.0.

See also: [set_device_pixel_ratio()] [ImageReader]

Sets the device pixel ratio for the pixmap. This is the ratio between image pixels and device-independent pixels.

The default scaleFactor is 1.0. Setting it to something else has two effects:

QPainters that are opened on the pixmap will be scaled. For example, painting on a 200x200 image if with a ratio of 2.0 will result in effective (device-independent) painting bounds of 100x100.

Code paths in Qt that calculate layout geometry based on the pixmap size will take the ratio into account: QSize layoutSize = pixmap.size() / pixmap.devicePixelRatio() The net effect of this is that the pixmap is displayed as high-DPI pixmap rather than a large pixmap (see Drawing High Resolution Versions of Pixmaps and Images ).

See also: [device_pixel_ratio()]

Returns true if this pixmap has an alpha channel, or has a mask, otherwise returns false.

See also: [has_alpha_channel()] [mask()]

Returns true if the pixmap has a format that respects the alpha channel, otherwise returns false.

See also: [has_alpha()]

Returns true if the pixmap has a format that respects the alpha channel, otherwise returns false.

See also: [has_alpha()]

Creates and returns a heuristic mask for this pixmap.

The function works by selecting a color from one of the corners and then chipping away pixels of that color, starting at all the edges. If clipTight is true (the default) the mask is just large enough to cover the pixels; otherwise, the mask is larger than the data pixels.

The mask may not be perfect but it should be reasonable, so you can do things such as the following:

This function is slow because it involves converting to/from a QImage, and non-trivial computations.

See also: Image::create_heuristic_mask [create_mask_from_color()]

Creates and returns a mask for this pixmap based on the given maskColor. If the mode is Qt::MaskInColor, all pixels matching the maskColor will be transparent. If mode is Qt::MaskOutColor, all pixels matching the maskColor will be opaque.

This function is slow because it involves converting to/from a QImage.

See also: [create_heuristic_mask()] [Image::create_mask_from_color]

width, int height)

Creates and returns a pixmap constructed by grabbing the contents of the given window restricted by QRect( x, y, width, height).

The arguments ( x , y ) specify the offset in the window, whereas ( width , height ) specify the area to be copied. If width is negative, the function copies everything to the right border of the window. If height is negative, the function copies everything to the bottom of the window.

The window system identifier ( WId) can be retrieved using the QWidget::winId() function. The rationale for using a window identifier and not a QWidget, is to enable grabbing of windows that are not part of the application, window system frames, and so on.

The grabWindow() function grabs pixels from the screen, not from the window, i.e. if there is another window partially or entirely over the one you grab, you get pixels from the overlying window, too. The mouse cursor is generally not grabbed.

Note on X11 that if the given window doesn’t have the same depth as the root window, and another window partially or entirely obscures the one you grab, you will not get pixels from the overlying window. The contents of the obscured areas in the pixmap will be undefined and uninitialized.

On Windows Vista and above grabbing a layered window, which is created by setting the Qt::WA_TranslucentBackground attribute, will not work. Instead grabbing the desktop widget should work.

Warning: In general, grabbing an area outside the screen is not safe. This depends on the underlying window system.

Warning: The function is deprecated in Qt 5.0 since there might be platform plugins in which window system identifiers ( WId) are local to a screen. Use QScreen::grabWindow() instead.

See also: [grab_widget()] {Screenshot Example} See also: Screen

Use QWidget::grab() instead.

Use QWidget::grab() instead.

Use QWidget::grab() instead.

Use QWidget::grab() instead.

Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const

Overloads Returns a copy of the pixmap scaled to a rectangle with the given width and height according to the given aspectRatioMode and transformMode.

If either the width or the height is zero or negative, this function returns a null pixmap.

aspectRatioMode, Qt::TransformationMode transformMode) const

Scales the pixmap to the given size, using the aspect ratio and transformation modes specified by aspectRatioMode and transformMode.

qimage-scaling.png

  • If aspectRatioMode is Qt::IgnoreAspectRatio, the pixmap is scaled to size.
  • If aspectRatioMode is Qt::KeepAspectRatio, the pixmap is scaled to a rectangle as large as possible inside size, preserving the aspect ratio.
  • If aspectRatioMode is Qt::KeepAspectRatioByExpanding, the pixmap is scaled to a rectangle as small as possible outside size, preserving the aspect ratio.

If the given size is empty, this function returns a null pixmap.

In some cases it can be more beneficial to draw the pixmap to a painter with a scale set rather than scaling the pixmap. This is the case when the painter is for instance based on OpenGL or when the scale factor changes rapidly.

See also: [is_null()] {QPixmap#Pixmap Transformations}{Pixmap Transformations}

mode) const

Returns a scaled copy of the image. The returned image is scaled to the given width using the specified transformation mode. The height of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved.

If width is 0 or negative, a null pixmap is returned.

See also: [is_null()] {QPixmap#Pixmap Transformations}{Pixmap Transformations}

Qt::TransformationMode mode) const

Returns a scaled copy of the image. The returned image is scaled to the given height using the specified transformation mode. The width of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved.

If height is 0 or negative, a null pixmap is returned.

See also: [is_null()] {QPixmap#Pixmap Transformations}{Pixmap Transformations}

Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const

Overloads Returns a copy of the pixmap scaled to a rectangle with the given width and height according to the given aspectRatioMode and transformMode.

If either the width or the height is zero or negative, this function returns a null pixmap.

aspectRatioMode, Qt::TransformationMode transformMode) const

Scales the pixmap to the given size, using the aspect ratio and transformation modes specified by aspectRatioMode and transformMode.

qimage-scaling.png

  • If aspectRatioMode is Qt::IgnoreAspectRatio, the pixmap is scaled to size.
  • If aspectRatioMode is Qt::KeepAspectRatio, the pixmap is scaled to a rectangle as large as possible inside size, preserving the aspect ratio.
  • If aspectRatioMode is Qt::KeepAspectRatioByExpanding, the pixmap is scaled to a rectangle as small as possible outside size, preserving the aspect ratio.

If the given size is empty, this function returns a null pixmap.

In some cases it can be more beneficial to draw the pixmap to a painter with a scale set rather than scaling the pixmap. This is the case when the painter is for instance based on OpenGL or when the scale factor changes rapidly.

See also: [is_null()] {QPixmap#Pixmap Transformations}{Pixmap Transformations}

mode) const

Returns a scaled copy of the image. The returned image is scaled to the given width using the specified transformation mode. The height of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved.

If width is 0 or negative, a null pixmap is returned.

See also: [is_null()] {QPixmap#Pixmap Transformations}{Pixmap Transformations}

Qt::TransformationMode mode) const

Returns a scaled copy of the image. The returned image is scaled to the given height using the specified transformation mode. The width of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved.

If height is 0 or negative, a null pixmap is returned.

See also: [is_null()] {QPixmap#Pixmap Transformations}{Pixmap Transformations}

mode) const

Returns a scaled copy of the image. The returned image is scaled to the given width using the specified transformation mode. The height of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved.

If width is 0 or negative, a null pixmap is returned.

See also: [is_null()] {QPixmap#Pixmap Transformations}{Pixmap Transformations}

Qt::TransformationMode mode) const

Returns a scaled copy of the image. The returned image is scaled to the given height using the specified transformation mode. The width of the pixmap is automatically calculated so that the aspect ratio of the pixmap is preserved.

If height is 0 or negative, a null pixmap is returned.

See also: [is_null()] {QPixmap#Pixmap Transformations}{Pixmap Transformations}

Converts the pixmap to a QImage. Returns a null image if the conversion fails.

If the pixmap has 1-bit depth, the returned image will also be 1 bit deep. Images with more bits will be returned in a format closely represents the underlying system. Usually this will be QImage::Format_ARGB32_Premultiplied for pixmaps with an alpha and QImage::Format_RGB32 or QImage::Format_RGB16 for pixmaps without alpha.

Note that for the moment, alpha masks on monochrome images are ignored.

See also: [from_image()] {QImage#Image Formats}{Image Formats}

Converts the given image to a pixmap using the specified flags to control the conversion. The flags argument is a bitwise-OR of the Qt::ImageConversionFlags . Passing 0 for flags sets all the default options.

In case of monochrome and 8-bit images, the image is first converted to a 32-bit pixmap and then filled with the colors in the color table. If this is too expensive an operation, you can use QBitmap::fromImage() instead.

See also: [from_image_reader()] [to_image()] {QPixmap#Pixmap Conversion}{Pixmap Conversion}

Overloads Converts the given image to a pixmap without copying if possible.

Create a QPixmap from an image read directly from an imageReader. The flags argument is a bitwise-OR of the Qt::ImageConversionFlags

Passing 0 for flags sets all the default options.

On some systems, reading an image directly to QPixmap can use less memory than reading a QImage to convert it to QPixmap.

See also: [from_image()] [to_image()] {QPixmap#Pixmap Conversion}{Pixmap Conversion}

Create a QPixmap from an image read directly from an imageReader. The flags argument is a bitwise-OR of the Qt::ImageConversionFlags

Passing 0 for flags sets all the default options.

On some systems, reading an image directly to QPixmap can use less memory than reading a QImage to convert it to QPixmap.

See also: [from_image()] [to_image()] {QPixmap#Pixmap Conversion}{Pixmap Conversion}

Converts the given image to a pixmap using the specified flags to control the conversion. The flags argument is a bitwise-OR of the Qt::ImageConversionFlags . Passing 0 for flags sets all the default options.

In case of monochrome and 8-bit images, the image is first converted to a 32-bit pixmap and then filled with the colors in the color table. If this is too expensive an operation, you can use QBitmap::fromImage() instead.

See also: [from_image_reader()] [to_image()] {QPixmap#Pixmap Conversion}{Pixmap Conversion}

Overloads Converts the given image to a pixmap without copying if possible.

Create a QPixmap from an image read directly from an imageReader. The flags argument is a bitwise-OR of the Qt::ImageConversionFlags

Passing 0 for flags sets all the default options.

On some systems, reading an image directly to QPixmap can use less memory than reading a QImage to convert it to QPixmap.

See also: [from_image()] [to_image()] {QPixmap#Pixmap Conversion}{Pixmap Conversion}

Replaces this pixmap’s data with the given image using the specified flags to control the conversion. The flags argument is a bitwise-OR of the Qt::ImageConversionFlags

Passing 0 for flags sets all the default options. Returns true if the result is that this pixmap is not null.

Note: this function was part of Qt 3 support in Qt 4.6 and earlier. It has been promoted to official API status in 4.7 to support updating the pixmap’s image without creating a new QPixmap as fromImage() would.

See also: [from_image()]

Overloads Returns a deep copy of the subset of the pixmap that is specified by the rectangle QRect( x, y, width, height).

Returns a deep copy of the subset of the pixmap that is specified by the given rectangle. For more information on deep copies, see the Implicit Data Sharing documentation.

If the given rectangle is empty, the whole image is copied.

See also: [operator()] [q_pixmap()] {QPixmap#Pixmap Transformations}{Pixmap Transformations}

Overloads Returns a deep copy of the subset of the pixmap that is specified by the rectangle QRect( x, y, width, height).

Returns a deep copy of the subset of the pixmap that is specified by the given rectangle. For more information on deep copies, see the Implicit Data Sharing documentation.

If the given rectangle is empty, the whole image is copied.

See also: [operator()] [q_pixmap()] {QPixmap#Pixmap Transformations}{Pixmap Transformations}

This convenience function is equivalent to calling QPixmap::scroll( dx, dy, QRect( x, y, width, height), exposed).

See also: Widget::scroll [GraphicsItem::scroll]

Scrolls the area rect of this pixmap by ( dx, dy). The exposed region is left unchanged. You can optionally pass a pointer to an empty QRegion to get the region that is exposed by the scroll operation.

You cannot scroll while there is an active painter on the pixmap.

See also: Widget::scroll [GraphicsItem::scroll]

This convenience function is equivalent to calling QPixmap::scroll( dx, dy, QRect( x, y, width, height), exposed).

See also: Widget::scroll [GraphicsItem::scroll]

Scrolls the area rect of this pixmap by ( dx, dy). The exposed region is left unchanged. You can optionally pass a pointer to an empty QRegion to get the region that is exposed by the scroll operation.

You cannot scroll while there is an active painter on the pixmap.

See also: Widget::scroll [GraphicsItem::scroll]

Returns a number that identifies this QPixmap. Distinct QPixmap objects can only have the same cache key if they refer to the same contents.

The cacheKey() will change when the pixmap is altered.

Detaches the pixmap from shared pixmap data.

A pixmap is automatically detached by Qt whenever its contents are about to change. This is done in almost all QPixmap member functions that modify the pixmap (fill(), fromImage(), load(), etc.), and in QPainter::begin() on a pixmap.

There are two exceptions in which detach() must be called explicitly, that is when calling the handle() or the x11PictureHandle() function (only available on X11). Otherwise, any modifications done using system calls, will be performed on the shared data.

The detach() function returns immediately if there is just a single reference or if the pixmap has not been initialized yet.

Returns true if this is a QBitmap; otherwise returns false.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.