2D绘图
之前使用了 QPainter 在 QWidget 上绘图, QPainter其实 还能在任何 QPaintDevice上绘制, 继承至QPaintDevice 还有QPixMap、QImage、QPicture,那么它们的区别是.
1、使用 QPixMap 进行绘图,根据硬件显示进行优化.
2、使用 QImage 进行绘图,独立与硬件,保存最原始的图片数据.
3、QPicture,可以记录和重现 QPainter 命令
将所有需要绘制的图形,先绘制在 QPixmap QImage QPicture 中, 绘制完成后,再到paintEvent 中一次性绘制出来,可以大大提高 painterEvent 事件处理器的处理效率,可以有效避免绘制过程出现卡顿。
(1)QPixmap与QImage
- QPixmap:先把所有的图形元素都画在 QPixmap 对象上面,然后再通过调用 update(), 去调度 paintEvent绘制出 QPixmap 对象的内容。QPixmap 对象可以使用save(),来保存成一个图片文件。
- QImage:先把所有的图形元素都画在 QPixmap 对象上面,然后再通过调用 update(), 去调度 paintEvent绘制出 QPixmap 对象的内容。QPixmap 对象可以使用save(),来保存成一个图片文件。
1)widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void paintEvent(QPaintEvent *event);
private:
QPixmap* _pixmap;
QImage* _image;
};
#endif // WIDGET_H
2)widget.cpp
#include "widget.h"
#include <QPixmap>
#include <QPainter>
#include <QImage>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
_pixmap = new QPixmap("E:/456.jpg"); //会根据硬件设备进行优化,加载的图像路径
_image = new QImage("E:/456.jpg"); //提供像素级别的图像出来
}
void Widget::paintEvent(QPaintEvent *event) {
QPainter painter;
painter.begin(this);
/* painter.drawPixmap(0,0,_pixmap->scaled(this->width(),this->height()
,Qt::KeepAspectRatioByExpanding
,Qt::SmoothTransformation)); */ //设置图片的缩放缩放的刷新
painter.drawImage(0,0,_image->scaled(this->width(),this->height()
,Qt::KeepAspectRatioByExpanding
,Qt::SmoothTransformation));
painter.end();
}
Widget::~Widget()
{
delete _pixmap;
}
(2)QPicture
先把所有的图形元素都画在QPicture对象上面,然后再通过调用update(),去调度paintEvent绘制出QPicture对象的内容。QPicture对象可以使用save(),来保存成一个文件。与QImage QPixmap的不同之处在于,QPicture强调的画图的动作的记录, 然后保存的文件不是一张图片。
1)widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void paintEvent(QPaintEvent *event);
public slots:
void drawPixmap();
private:
QPixmap* _pixmap;
};
#endif // WIDGET_H
2)widget.cpp
#include "widget.h"
#include <QPixmap>
#include <QPainter>
#include <QPushButton>
#include <QImage>
#include <QDialog>
#include <QHBoxLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
_pixmap = new QPixmap(this->size()); //会根据硬件设备进行优化,加载的图像路径
_pixmap->fill(QColor("#00000000"));
QDialog* _ctrlDialog = new QDialog(this);
QHBoxLayout* hBox = new QHBoxLayout(_ctrlDialog);
QPushButton* pb = new QPushButton("drawPixmap");
hBox->addWidget(pb);
connect(pb,SIGNAL(clicked()),this,SLOT(drawPixmap()));
_ctrlDialog->show();
}
void Widget::drawPixmap() {
QPainter painter;
painter.begin(_pixmap);
painter.setPen(QPen(Qt::red));
painter.setBrush(QBrush(Qt::blue));
painter.drawLine(0,0,100,100);
painter.drawRect(100,100,100,100);
painter.drawEllipse(200,200,100,100);
painter.drawText(100,100,"hello world");
painter.end();
_pixmap->save("E:/1111.png"); //保存图片
this->update();
}
void Widget::paintEvent(QPaintEvent *event) {
QPainter painter;
painter.begin(this);
painter.drawPixmap(0, 0, *_pixmap);
painter.end();
}
Widget::~Widget()
{
delete _pixmap;
}