发布时间:2023-12-12 15:30
#include \"mainwindow.h\"
#include \"ui_mainwindow.h\"
#include \"QPixmap\"
#include \"QPainter\"
#include \"QImage\"
#include \"QPicture\"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//pixmap绘图设备 可以保存在磁盘上 专门为 平台做了优化
QPixmap pix(300,300);
//填充颜色
pix.fill(Qt::white);
//声明画家
QPainter painter(&pix);
QPen pen(Qt::green);
pen.setWidth(5);
painter.setPen(pen);
painter.drawEllipse(QPoint(100,100),50,50);
//保存
pix.save(\"E:/pix.png\");
//QImage绘图设备
QImage img(QSize(300,300),QImage::Format_RGB32);
img.fill(Qt::white);
//声明画家
QPainter painter2(&img);
QPen pen2(Qt::yellow);
pen2.setWidth(5);
painter2.setPen(pen2);
painter2.drawEllipse(QPoint(100,100),50,50);
//保存
img.save(\"E:/img.png\");
//Qpicture绘图设备 可以记录和重现绘图指令
QPicture pic;
QPainter painter3;
//开始往pic上绘图
painter3.begin(&pic);
QPen pen3(Qt::red);
pen3.setWidth(5);
painter3.setPen(pen3);
painter3.drawEllipse(QPoint(100,300),50,50);
//结束绘图
painter3.end();
//保存
pic.save(\"E:/pic.xyx\");
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
//利用QImage对像素进行修改
QImage img;
img.load(\":/img/back1.jpg\");
//修改像素点
for(int i=50;i<100;i++)
{
for(int j=50;j<100;j++)
{
QRgb value = qRgb(255,0,0);
img.setPixel(i,j,value);
}
}
painter.drawImage(0,0,img);
//重现pic的绘图指令
QPicture pic;
pic.load(\"E:/pic.xyx\");
painter.drawPicture(0,0,pic);
}
MainWindow::~MainWindow()
{
delete ui;
}