1 绘图设备

绘图设备: 继承QPainterDevice的子类
Qt中一共提供了四种绘图设备, 分别是QPixmap, QBitmap, QImage, QPicture

(1) QPixmap

QPixmap用于图像在屏幕上的显示

  • QBitmap是QPixmap的子类, 它的色深限定为1, 即只有黑白两种颜色, 一般这种图被称为位图

    (2) QImage

    QImage用于图像的像素级访问

    (3) QPicture

    QPicture可以记录和重现QPainter的各条命令 ```cpp Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this);

    // QPixmap 绘图设备,优化不同平台的显示 //QPixmap pix(300,300); //pix.fill(Qt::white); // 设置填充色为白色 //QPainter painter(&pix); //painter.setPen(QPen(Qt::green)); //painter.drawEllipse(QPoint(150,150),100,100); //pix.save(“E:/pix.png”);

    // QImage 绘图设备,优化像素级访问 //QImage img(300,300,QImage::Format_RGB32); //img.fill(Qt::white); //QPainter painter(&img); //painter.setPen(QPen(Qt::blue)); //painter.drawEllipse(QPoint(150,150),100,100); //img.save(“E:/img.png”);

    // QPicture 绘图设备,重新记录绘图指令 QPicture pic; QPainter painter; painter.begin(&pic); painter.setPen(QPen(Qt::cyan)); painter.drawEllipse(QPoint(150,150),100,100); painter.end(); pic.save(“E:/pic.ws”); }

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

void Widget::paintEvent(QPaintEvent *) { //———————————QImage修改像素点————————————// //QImage img; //img.load(“:/pic/Image/002.png”); //for (int i = 0; i < 50; i++) { // for (int j = 0; j < 50; ++j) { // QRgb value = qRgb(0,255,0); // img.setPixel(i,j,value); // } //} //QPainter painter(this); //painter.drawImage(QPoint(0,0),img); //———————————QPicture重现绘图指令————————————// QPicture pic; pic.load(“E:/pic.ws”); QPainter painter(this); painter.drawPicture(0,0,pic); } ```