绘图设备
QPixmap QBitMap(黑色),QPicture,QWidget
QPixmap
创建对象 QPixmap pix(w,h)
fill填充
保存 save
QImage img ( w,h ,format)设置像素色 setPixel
QPicture 重现和记录 绘图指令 后缀名随便写 无要求
#-------------------------------------------------
#
# Project created by QtCreator 2019-08-17T14:29:11
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 04_QPainterDevice
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
res.qrc
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
public:
//绘图
void paintEvent(QPaintEvent *);
};
#endif // WIDGET_H
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "widget.h"
#include "ui_widget.h"
#include <QPixmap>
#include <QPainter>
#include <QImage>
#include <QPicture>
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("D:/pix.png");//保存到本地
//QImage 做绘图设备 对像素级访问做了优化
//参数3,format通常用RGB32 或 ARGB32色味
QImage img(300,300,QImage::Format_RGB32);//并没有直接宽高 重载
img.fill(Qt::white);//填充白色
QPainter painter1(&img);
painter1.setPen(QPen(Qt::blue));
painter1.drawEllipse(QPoint(150,150),100,100);
img.save("D:/img.png");//保存到本地
QPicture pic;//用于重现记录绘图指令的。
QPainter painter2;
painter2.begin(&pic);
painter2.setPen(QPen(Qt::red));
painter2.drawEllipse(QPoint(100,100),100,100);
painter2.end();
// 保存
pic.save("D:/pic.zz");//记录指令 不是用来打开的,可以重现
//后缀名随便写
//QImage 对像素级做了优化
}
void Widget::paintEvent(QPaintEvent *){
//重现绘图指令 QPictue;
QPicture pic;
pic.load("D:\\pic.zz");
QPainter painter(this);
painter.drawPicture(0,0,pic);
//QPicture,记录指令 重现指令,并不是用来画图的
//QImage 可以修改像素
// QImage img;
// img.load(":/Image/Luffy.png");
// for(int i=50;i<150;i++){
// for(int j=50;j<150;j++){
// QRgb value=qRgb(0,255,0);
// img.setPixel(i,j,value);//设置像素点颜色
// }
// }
// QPainter painter(this);
// painter.drawImage(QPoint(0,0),img);
}
Widget::~Widget()
{
delete ui;
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>