Qt 绘图设备:

工程文件:

widget.cpp:

#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QPixmap>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //由于不是在窗口中进行绘图,即不用在piantEvent 中实现,在QPixmap的设备中实现,这里在构造函数中实现就可以了,
    //绘图设备 ,400*300
    QPixmap pixmap(400,300);

    QPainter p(&pixmap);

    //填充白色的背景
//    p.fillRect(0,0,400,300,QBrush(Qt::white));
    pixmap.fill(Qt::white);

    //画一张图片并保存
    p.drawPixmap(0,0,80,80,QPixmap("://res/2.png"));

    pixmap.save("../pixmap.png");
}

Widget::~Widget()
{
    delete ui;
}

/*
 * 绘图设备:
 * QPixmap:针对屏幕进行优化了,和平台相关(显卡),不能对图片进行修改
 * QImage:和平台无关,可以对图片进行修改,在线程中绘图(例如在地图的导航应用中)
 * QPicture:保存绘图的状态(二进制文件)
*/

猜你喜欢

转载自www.cnblogs.com/doker/p/11137552.html