QT学习笔记(十三):绘制图像
paintEvent() 事件源码添加:
#include <QPainter>
#include <QImage>
#include <QPixmap>
#include <QBitmap>
#include <QPicture>
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter;
QImage 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();
QPixmap 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();
QBitmap 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();
QPicture 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();
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;
QPixmap grab = window.screen()->grabWindow(QApplication::desktop()->winId(),0,0,500,500);
grab.save("../mydrawing/screen.png");
QLabel *label = new QLabel(this);
label->resize(400, 200);
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);
picture.save("../mydrawing/drawing.pic");
QPicture picture2;
picture2.load("../mydrawing/drawing.pic");
painter.begin(this);
painter.drawPicture(100,0,picture2);
结果: