QPicture Class
QPicture 类是一个绘图设备,用于记录和重放QPainter 命令。更多内容...
| 头文件: | #include <QPicture> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
| qmake: | QT += gui |
| 继承自: | QPaintDevice |
- 所有成员列表(包括继承的成员)
- QPicture 属于“隐式共享类”。
公共函数
| 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) |
详细说明
图片将绘图命令以一种与平台无关的格式序列化到 I/O 设备中。它们有时也被称为元文件。
Qt 图片采用专有的二进制格式。与许多窗口系统上的原生图片(元文件)格式不同,Qt 图片在内容方面没有任何限制。凡是可以在小部件或位图上绘制的内容(例如,字体、位图、区域、变换后的图形等),都可以存储在图片中。
QPicture 是不依赖分辨率的,即一个 QPicture 可以在不同的设备(例如 svg、pdf、ps、打印机和屏幕)上显示,且外观完全一致。例如,这对于所见即所得(WYSIWYG)打印预览是必不可少的。 QPicture 以默认系统 dpi 运行,并根据窗口系统自动缩放绘图对象以适应分辨率差异。
记录图片的示例:
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请注意,每次调用QPainter::begin()函数时,绘图命令列表都会被重置。
图片回放示例:
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也可以使用play() 绘制图片。可获取有关图片的一些基本数据,例如size()、isNull() 和boundingRect()。
注意:QPicture 使用QDataStream 进行序列化。关于读取不可信数据的same reservations 同样适用。
另请参阅 QMovie 。
成员函数文档
[explicit] QPicture::QPicture(int formatVersion = -1)
创建一个空的图片。
可使用formatVersion 参数来创建一个QPicture,该对象可被使用早期版本Qt编译的应用程序读取。
请注意,默认的 formatVersion 值为 -1,这表示当前版本;也就是说,对于 Qt 4.0,formatVersion 值为 7 与默认值 -1 相同。
Qt 4.0 不支持读取由早期版本的 Qt 生成的图片。
QPicture::QPicture(const QPicture &pic)
创建pic 的副本。
得益于隐式共享,此构造函数运行迅速。
[virtual noexcept] QPicture::~QPicture()
毁了这幅画。
QRect QPicture::boundingRect() const
返回图片的边界矩形;如果图片中没有数据,则返回一个无效的矩形。
另请参阅 ` setBoundingRect()`。
const char *QPicture::data() const
返回指向图片数据的指针。该指针仅在对该图片调用下一个非const函数之前有效。如果图片不包含任何数据,则返回的指针值为0。
另请参阅 setData()、size(),以及isNull()。
bool QPicture::isNull() const
如果图片中不包含数据,则返回true ;否则返回false。
bool QPicture::load(const QString &fileName)
从fileName 指定的文件中加载图片,若成功则返回true;否则将图片标记为无效,并返回false 。
另请参阅 save()。
bool QPicture::load(QIODevice *dev)
dev 是用于加载的设备。
这是一个重载函数。
bool QPicture::play(QPainter *painter)
使用painter 重放图片,若成功则返回true ;否则返回false 。
该函数的功能与QPainter::drawPicture()完全相同,其中(x, y) = (0, 0)。
注意: 此函数不会保留绘图器的状态。
bool QPicture::save(const QString &fileName)
将图片保存到由 `fileName ` 指定的文件中,如果成功则返回 `true`;否则返回 `false`。
另请参阅 load()。
bool QPicture::save(QIODevice *dev)
dev 是用于保存的设备。
这是一个重载函数。
void QPicture::setBoundingRect(const QRect &r)
将图片的边界矩形设置为r 。系统会覆盖自动计算出的值。
另请参阅 boundingRect()。
[virtual] void QPicture::setData(const char *data, uint size)
直接从data 和size 设置图像数据。此函数会复制输入数据。
uint QPicture::size() const
返回图片数据的大小。
另请参阅 data()。
[noexcept] void QPicture::swap(QPicture &other)
将这张图片与other 互换。此操作速度极快,且从不会失败。
[noexcept] QPicture &QPicture::operator=(QPicture &&other)
将other 通过Move操作赋值给此QPicture 实例。
QPicture &QPicture::operator=(const QPicture &p)
将图片p 分配给此图片,并返回对此图片的引用。
相关非成员
QDataStream &operator<<(QDataStream &s, const QPicture &r)
将图片r 写入流s ,并返回该流的引用。
QDataStream &operator>>(QDataStream &s, QPicture &r)
从流s 读取图片到图片r ,并返回该流的引用。
© 2026 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.