QPicture Class

The QPicture class is a paint device that records and replays QPainter commands. More...

Header: #include <QPicture>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
Inherits: QPaintDevice

Public Functions

QPicture(int formatVersion = -1)
QPicture(const QPicture &pic)
virtual ~QPicture()
QRect boundingRect() const
const char *data() const
bool isNull() const
bool load(const QString &fileName)
bool load(QIODevice *dev)
bool play(QPainter *painter)
bool save(const QString &fileName)
bool save(QIODevice *dev)
void setBoundingRect(const QRect &r)
virtual void setData(const char *data, uint size)
uint size() const
void swap(QPicture &other)
QPicture &operator=(QPicture &&other)
QPicture &operator=(const QPicture &p)
QDataStream &operator<<(QDataStream &s, const QPicture &r)
QDataStream &operator>>(QDataStream &s, QPicture &r)

Detailed Description

A picture serializes painter commands to an IO device in a platform-independent format. They are sometimes referred to as meta-files.

Qt pictures use a proprietary binary format. Unlike native picture (meta-file) formats on many window systems, Qt pictures have no limitations regarding their contents. Everything that can be painted on a widget or pixmap (e.g., fonts, pixmaps, regions, transformed graphics, etc.) can also be stored in a picture.

QPicture is resolution independent, i.e. a QPicture can be displayed on different devices (for example svg, pdf, ps, printer and screen) looking the same. This is, for instance, needed for WYSIWYG print preview. QPicture runs in the default system dpi, and scales the painter to match differences in resolution depending on the window system.

Example of how to record a picture:

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

Note that the list of painter commands is reset on each call to the QPainter::begin() function.

Example of how to replay a 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

Pictures can also be drawn using play(). Some basic data about a picture is available, for example, size(), isNull() and boundingRect().

See also QMovie.

Member Function Documentation

[explicit] QPicture::QPicture(int formatVersion = -1)

Constructs an empty picture.

The formatVersion parameter may be used to create a QPicture that can be read by applications that are compiled with earlier versions of Qt.

Note that the default formatVersion is -1 which signifies the current release, i.e. for Qt 4.0 a formatVersion of 7 is the same as the default formatVersion of -1.

Reading pictures generated by earlier versions of Qt is not supported in Qt 4.0.

QPicture::QPicture(const QPicture &pic)

Constructs a copy of pic.

This constructor is fast thanks to implicit sharing.

[virtual noexcept] QPicture::~QPicture()

Destroys the picture.

QRect QPicture::boundingRect() const

Returns the picture's bounding rectangle or an invalid rectangle if the picture contains no data.

See also setBoundingRect().

const char *QPicture::data() const

Returns a pointer to the picture data. The pointer is only valid until the next non-const function is called on this picture. The returned pointer is 0 if the picture contains no data.

See also setData(), size(), and isNull().

bool QPicture::isNull() const

Returns true if the picture contains no data; otherwise returns false.

bool QPicture::load(const QString &fileName)

Loads a picture from the file specified by fileName and returns true if successful; otherwise invalidates the picture and returns false.

See also save().

bool QPicture::load(QIODevice *dev)

This is an overloaded function.

dev is the device to use for loading.

bool QPicture::play(QPainter *painter)

Replays the picture using painter, and returns true if successful; otherwise returns false.

This function does exactly the same as QPainter::drawPicture() with (x, y) = (0, 0).

Note: The state of the painter isn't preserved by this function.

bool QPicture::save(const QString &fileName)

Saves a picture to the file specified by fileName and returns true if successful; otherwise returns false.

See also load().

bool QPicture::save(QIODevice *dev)

This is an overloaded function.

dev is the device to use for saving.

void QPicture::setBoundingRect(const QRect &r)

Sets the picture's bounding rectangle to r. The automatically calculated value is overridden.

See also boundingRect().

[virtual] void QPicture::setData(const char *data, uint size)

Sets the picture data directly from data and size. This function copies the input data.

See also data() and size().

uint QPicture::size() const

Returns the size of the picture data.

See also data().

[noexcept] void QPicture::swap(QPicture &other)

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

[noexcept] QPicture &QPicture::operator=(QPicture &&other)

Move-assigns other to this QPicture instance.

QPicture &QPicture::operator=(const QPicture &p)

Assigns picture p to this picture and returns a reference to this picture.

Related Non-Members

QDataStream &operator<<(QDataStream &s, const QPicture &r)

Writes picture r to the stream s and returns a reference to the stream.

QDataStream &operator>>(QDataStream &s, QPicture &r)

Reads a picture from the stream s into picture r and returns a reference to the stream.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.