QT学习笔记(十三):绘制图像

QT学习笔记(十三):绘制图像

QT学习笔记(十三):绘制图像_第1张图片
paintEvent() 事件源码添加:

#include <QPainter>
#include <QImage>
#include <QPixmap>
#include <QBitmap>
#include <QPicture>

void Widget::paintEvent(QPaintEvent *)
{
    
    QPainter painter;
    
    // 绘制image
    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();
    
    // 绘制pixmap
    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();
    
    // 绘制bitmap
    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();
    
    // 绘制picture
    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();
    
    // 在widget部件上进行绘制
    painter.begin(this);
    painter.drawImage(50, 20, image);
    painter.drawPixmap(200, 20, pix); 
    painter.drawPixmap(50, 170, bit);		// 只有黑白两色,只显示轮廓
    painter.drawPicture(200, 170, picture);
    
}

结果
QT学习笔记(十三):绘制图像_第2张图片
QT学习笔记(十三):绘制图像_第3张图片

1.QImage 类

QT学习笔记(十三):绘制图像_第4张图片
QT学习笔记(十三):绘制图像_第5张图片

#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");
}

结果:
QT学习笔记(十三):绘制图像_第6张图片

1.QPixmap 类

QT学习笔记(十三):绘制图像_第7张图片

#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");
    // 创建 label
    QLabel *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);
}

结果:
QT学习笔记(十三):绘制图像_第8张图片
QT学习笔记(十三):绘制图像_第9张图片
QT学习笔记(十三):绘制图像_第10张图片

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());
}

结果:
QT学习笔记(十三):绘制图像_第11张图片

3.QPicture 类

QT学习笔记(十三):绘制图像_第12张图片

#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);

结果:
QT学习笔记(十三):绘制图像_第13张图片

你可能感兴趣的:(QT)