QPainter绘图
- 重写绘图事件,虚函数
- 如果窗口绘图,必须放在绘图事件里实现
- 绘图事件内部自动调用,窗口需要重绘的时候,状态改变
绘图设备(QPixmap,QImage,QBitmap,QPicture)
QPixmap图片背景透明,针对屏幕进行优化了,和平台相关,不能对图片进行修改
QImage 和平台无关,可以对图片进行修改,在线程中绘图
QPicture 保存绘图 的状态(二进制文件)
pixmap.save("../pixmap.png"); 保存图片
setWindowFlags(Qt::FramelessWindowHint | windowFlags());去除边框
setAttribute(Qt::WA_TranslucentBackground);设置透明
QPainter
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
protected:
void paintEvent(QPaintEvent *event);
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
int x=0;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QPen>
#include <QBrush>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
//QPainter p(this);
QPainter p;//创建画家对象
p.begin(this);//指定当前窗口为绘图设备
//画背景图
//p.drawPixmap(0,0, width(), height(), QPixmap("../Image/bk.png"));
//p.drawPixmap(rect(),QPixmap("../Image/bk.png"));
//定义画笔
QPen pen;
pen.setWidth(5);//设置线宽
pen.setColor(QColor(124,123,0));//rgb
pen.setStyle(Qt::DashDotDotLine);
QBrush brush;
brush.setColor(Qt::red);
brush.setStyle(Qt::Dense1Pattern);
//p.setPen(pen);
p.setBrush(brush);
//画直线
p.drawLine(50,50,150,50);
p.drawLine(50, 50,50,150 );
//画矩形
p.drawRect(x,150,100,50);
p.drawEllipse(QPoint(150,150),25,50);
p.drawPixmap(x,180,80,80,QPixmap("../Image/face.png"));
p.end();
}
void Widget::on_pushButton_clicked()
{
x += 20;
if(x>width())
{
x = 0;
}
update();
}
QBitmap
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
protected:
void paintEvent(QPaintEvent *event);
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QBitmap>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter p(this);
//QPixmap图片背景透明
p.drawPixmap(0,0,QPixmap("../Image/butterfly.png"));
//QBitmap
p.drawPixmap(200,0, QBitmap("../Image/butterfly.png"));
//QPixmap 图片背景白色
QPixmap pixmap;
pixmap.load("../Image/butterfly1.png");
}
QPixmap
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
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("../Image/face.png"));
//保存图片
pixmap.save("../pixmap.png");
}
Widget::~Widget()
{
delete ui;
}
QImage
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QImage image(400,300,QImage::Format_ARGB32);
QPainter p;
p.begin(&image);
p.drawLine(50,50,100,100);
for(int i=0;i<50;i++)
{
for(int j=0;j<50;j++)
{
image.setPixel(QPoint(i,j),qRgb(0,255,0));
}
}
p.end();
image.save("../image.png");
}
Widget::~Widget()
{
delete ui;
}
QPicture
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QPicture>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QPicture picture;
QPainter p;
p.begin(&picture);
p.drawLine(50,50,100,100);
p.end();
picture.save("../picture.png");
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
// QPicture pic;
// pic.load("../picture.png");
QPainter p(this);
// p.drawPicture(0,0,pic);
QPixmap pixmap;
pixmap.load("../Image/face.png");
QImage tempImage = pixmap.toImage();
p.drawImage(0,0, tempImage);
QImage image;
image.load("../Image/face.png");
QPixmap tempPixmap = QPixmap::fromImage(image);
}
窗口透明
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint | windowFlags());
setAttribute(Qt::WA_TranslucentBackground);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter p(this);
p.drawLine(50,50,100,100);
}
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::RightButton)
{
close();
qDebug()<<"close";
}
else if(event->button() == Qt::LeftButton)
{
p = event->globalPos() - this->frameGeometry().topLeft();
qDebug()<<"left";
}
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if(event->button() & Qt::LeftButton)
{
move(event->globalPos()-p);
}
}