QT学习笔记(十三):绘制图像
paintEvent() 事件源码添加:
#include <QPainter>
#include <QImage>
#include <QPixmap>
#include <QBitmap>
#include <QPicture>void Widget::paintEvent(QPaintEvent *)
{QPainter painter;// 绘制imageQImage image(100, 100, QImage::Format_ARGB32);painter.begin(&image); // 设备设定painter.setPen(QPen(Qt::green, 3));painter.setBrush(Qt::yellow);painter.drawRect(10, 10, 60, 60);painter.drawText(10, 10, 60, 60, Qt::AlignCenter, tr("QImage"));painter.setBrush(QColor(0 , 0, 0, 100));painter.drawRect(50, 50, 40, 40);painter.end();// 绘制pixmapQPixmap pix(100, 100);painter.begin(&pix);painter.setPen(QPen(Qt::green, 3));painter.setBrush(Qt::yellow);painter.drawRect(10, 10, 60, 60);painter.drawText(10, 10, 60, 60, Qt::AlignCenter, tr("QPixmap"));painter.setBrush(QColor(0 , 0, 0, 100));painter.drawRect(50, 50, 40, 40);painter.end();// 绘制bitmapQBitmap bit(100, 100);painter.begin(&bit);painter.setPen(QPen(Qt::green, 3));painter.setBrush(Qt::yellow);painter.drawRect(10, 10, 60, 60);painter.drawText(10, 10, 60, 60, Qt::AlignCenter, tr("QBitmap"));painter.setBrush(QColor(0 , 0, 0, 100));painter.drawRect(50, 50, 40, 40);painter.end();// 绘制pictureQPicture picture;painter.begin(&picture);painter.setPen(QPen(Qt::green, 3));painter.setBrush(Qt::yellow);painter.drawRect(10, 10, 60, 60);painter.drawText(10, 10, 60, 60, Qt::AlignCenter, tr("QPicture"));painter.setBrush(QColor(0 , 0, 0, 100));painter.drawRect(50, 50, 40, 40);painter.end();// 在widget部件上进行绘制painter.begin(this);painter.drawImage(50, 20, image);painter.drawPixmap(200, 20, pix); painter.drawPixmap(50, 170, bit); // 只有黑白两色,只显示轮廓painter.drawPicture(200, 170, picture);}
结果
1.QImage 类
#include <QImage>void Widget::paintEvent(QPaintEvent *)
{QPainter painter(this);QImage image;// 加载一张图片image.load("../mydrawing/image.png");// 输出图片的一些信息qDebug() << image.size() << image.format() << image.depth();// 在界面上绘制图片painter.drawImage(QPoint(10, 10), image);// 获取镜像图片(垂直镜像图片)QImage mirror = image.mirrored(false,true);// 将图片进行扭曲QTransform transform;transform.shear(0.2, 0);QImage image2 = mirror.transformed(transform);painter.drawImage(QPoint(10, 160), image2);// 将镜像图片保存到文件image2.save("../mydrawing/mirror.png");
}
结果:
1.QPixmap 类
#include <QPixmap>
#include <QDesktopWidget>
#include <QWindow>
#include <QSCreen>
#include <QLabel>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);QWindow window;// 截取屏幕上 (0,0,500,500) 矩形范围图像QPixmap grab = window.screen()->grabWindow(QApplication::desktop()->winId(),0,0,500,500);// 储存图片grab.save("../mydrawing/screen.png");// 创建 labelQLabel *label = new QLabel(this);label->resize(400, 200);// 缩放 pix(保证长宽比,平滑转换)QPixmap pix = grab.scaled(label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);// 添加图片label->setPixmap(pix);label->move(0, 50);
}
结果:
void Widget::paintEvent(QPaintEvent *)
{QPainter painter(this);QPixmap pix;// 加载图片pix.load("../mydrawing/yafeilinux.png");// 绘制图片,添加不同透明颜色矩形覆盖图形painter.drawPixmap(0, 0, pix.width(), pix.height(), pix);painter.setBrush(QColor(255, 255, 255, 100));painter.drawRect(0, 0, pix.width(), pix.height());// 绘制图片,添加不同透明颜色矩形覆盖图形painter.drawPixmap(100, 0, pix.width(), pix.height(), pix);painter.setBrush(QColor(0, 0, 255, 100));painter.drawRect(100, 0, pix.width(), pix.height());
}
结果:
3.QPicture 类
#include <QPicture>QPicture picture;
QPainter painter;
painter.begin(&picture);
painter.setPen(QPen(Qt::green, 3));
painter.setBrush(Qt::yellow);
painter.drawEllipse(10,20,80,70);
painter.end();
painter.begin(this);
painter.drawPicture(0, 0, picture);// 将 drawing.pic 绘图命令序列化到 I/O 设备中
picture.save("../mydrawing/drawing.pic");// 读取并重演 drawing.pic 绘图命令
QPicture picture2;
picture2.load("../mydrawing/drawing.pic");
painter.begin(this);
painter.drawPicture(100,0,picture2);
结果: