[QT][Drawing Fundamentals] Use of QPixmap, QImage, QPicture, QPainter

[QT][Drawing Fundamentals] Use of QPixmap, QImage, QPicture, QPainter

Rewrite the drawing event, draw in the window

//重写绘图事件,在窗口绘图必须在绘图事件里实现,窗口需要重绘的时候自动调用
//事件外调用update(),可手动刷新界面,间接调用paintEvent
void Widget::paintEvent(QPaintEvent *)
{
    
    
    QPainter painter(this);//指定当前窗口为绘图设备
    painter.drawPixmap(0,0,width(),height(),QPixmap("./images/bg.jpg"));//窗口有多大就画多大,相当于背景图
    //painter.drawPixmap(rect(),QPixmap("./images/bg.jpg"));//另一种写法,rect()直接获取窗口矩形区域
    painter.drawPixmap(200,0,QBitmap("./images/xiaolian.jpg"));//QBitmap绘图只有黑白两种颜色

    QPixmap pixmap;
    pixmap.load("./images/timg.jpg");//加载图片
    painter.drawPixmap(200,200,pixmap);

    QPen pen;//定义画笔
    pen.setColor(QColor(123,5,200));//设置画笔颜色
    pen.setWidth(5);//设置画笔宽度
    pen.setStyle(Qt::DotLine);//设置画笔风格

    QBrush brush;//定义画刷
    brush.setColor(Qt::yellow);//设置颜色
    brush.setStyle(Qt::DiagCrossPattern);//设置画刷风格
    painter.setBrush(brush);//把画刷交给画家

    painter.setPen(pen);//把画笔交给画家
    painter.drawLine(50,50,150,50);//画直线,起点坐标-终点坐标
    painter.drawRect(200,200,150,100);//画矩形,起点坐标-宽度-高度
    painter.drawEllipse(150,150,100,50);//画圆形,圆心坐标-水平半径-垂直半径
}

Use drawing equipment QPixmap, QImage, QPicture to draw

QPixmap: Optimized for the screen, and platform-dependent (different platforms may have different display effects), and the picture cannot be modified.
QImage: Platform-independent, the picture can be modified (pixels to modify the color), and you can draw in the thread
QPicture: Save the state of the drawing (binary file)

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    
    
    ui->setupUi(this);

    //QPixmap绘图
    QPixmap pixmap(400,300);//绘图设备,400*300
    QPainter painter(&pixmap);//创建画家
    painter.fillRect(0,0,400,300,QBrush(Qt::red));//填充白色背景
    //pixmap.fill(Qt::white);//另一种方式,填充白色背景
    painter.drawPixmap(0,0,80,80,QPixmap("./images/cake.png"));
    pixmap.save("../pixmap.jpg");//保存图片到本地

    //QImage绘图
    QImage image(400,300,QImage::Format_ARGB32);//绘图设备QImage,第三个参数是绘图样式,透明背景
    QPainter painterNUM2(&image);
    painterNUM2.drawImage(0,0,QImage("./images/timg.jpg"));
    for(int i=0;i<50;i++)
        for(int j=0;j<50;j++)
        {
    
    
            image.setPixel(i,j,qRgb(255,0,0));//按像素点修改图片颜色
        }
    image.save("../image.png");//保存图片到本地

    //QPicture绘图
    QPicture picture;
    QPainter painterNUM3;
    painterNUM3.begin(&picture);
    painterNUM3.drawPixmap(0,0,80,80,QPixmap("./images/timg.jpg"));
    painterNUM3.drawLine(50,50,150,50);
    painterNUM3.end();
    picture.save("../picture.png");//保存到本地,保存的是二进制文件,无法直接打开
}
//重写绘图事件,加载QPicture文件,QPixmap与QImage相互转换
void Widget::paintEvent(QPaintEvent *)
{
    
    
    QPicture picture2;
    picture2.load("../picture.png");//加载保存的QPicture二进制文件
    QPainter painterNUM4(this);//指定本窗口伟绘图设备
    painterNUM4.drawPicture(0,0,picture2);//在窗口绘制QPicture二进制文件

    //QPixmap与QImage相互转换
    QPixmap pixmap("./images/timg.jpg");
    QImage tempImage = pixmap.toImage();//QPixmap转换为QImage
    painterNUM4.drawImage(100,100,tempImage);

    QImage image;
    image.load("./images/timg.jpg");
    QPixmap tempPixmap = QPixmap::fromImage(image);//QImage转换为QPixmap
    painterNUM4.drawPixmap(0,100,tempPixmap);
}

Guess you like

Origin blog.csdn.net/weixin_40355471/article/details/106319049