一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
  • QPainter绘图绘图设备(QPixmap,QImage,QBitmap,QPicture)
    • 重写绘图事件,虚函数
    • 如果窗口绘图,必须放在绘图事件里实现
    • 绘图事件内部自动调用,窗口需要重绘的时候,状态改变
    • QPixmap图片背景透明,针对屏幕进行优化了,和平台相关,不能对图片进行修改
    • QImage 和平台无关,可以对图片进行修改,在线程中绘图
    • QPicture 保存绘图 的状态(二进制文件)
    • pixmap.save("../pixmap.png"); 保存图片
    • setWindowFlags(Qt::FramelessWindowHint | windowFlags());去除边框
    • setAttribute(Qt::WA_TranslucentBackground);设置透明

QPainter

 1 #ifndef WIDGET_H
 2 #define WIDGET_H
 3  
 4 #include <QWidget>
 5  
 6 namespace Ui {
 7 class Widget;
 8 }
 9  
10 class Widget : public QWidget
11 {
12     Q_OBJECT
13  
14 public:
15     explicit Widget(QWidget *parent = 0);
16     ~Widget();
17  
18 protected:
19     void paintEvent(QPaintEvent *event);
20  
21 private slots:
22     void on_pushButton_clicked();
23  
24 private:
25     Ui::Widget *ui;
26     int x=0;
27 };
28  
29 #endif // WIDGET_H
 1 #include "widget.h"
 2 #include "ui_widget.h"
 3 #include <QPainter>
 4 #include <QPen>
 5 #include <QBrush>
 6 Widget::Widget(QWidget *parent) :
 7     QWidget(parent),
 8     ui(new Ui::Widget)
 9 {
10     ui->setupUi(this);
11 }
12  
13 Widget::~Widget()
14 {
15     delete ui;
16 }
17  
18 void Widget::paintEvent(QPaintEvent *event)
19 {
20     //QPainter p(this);
21     QPainter p;//创建画家对象
22     p.begin(this);//指定当前窗口为绘图设备
23     //画背景图
24     //p.drawPixmap(0,0, width(), height(), QPixmap("../Image/bk.png"));
25     //p.drawPixmap(rect(),QPixmap("../Image/bk.png"));
26  
27     //定义画笔
28     QPen pen;
29     pen.setWidth(5);//设置线宽
30     pen.setColor(QColor(124,123,0));//rgb
31     pen.setStyle(Qt::DashDotDotLine);
32  
33     QBrush brush;
34     brush.setColor(Qt::red);
35     brush.setStyle(Qt::Dense1Pattern);
36  
37     //p.setPen(pen);
38     p.setBrush(brush);
39     //画直线
40     p.drawLine(50,50,150,50);
41     p.drawLine(50, 50,50,150 );
42  
43     //画矩形
44     p.drawRect(x,150,100,50);
45  
46     p.drawEllipse(QPoint(150,150),25,50);
47  
48     p.drawPixmap(x,180,80,80,QPixmap("../Image/face.png"));
49  
50     p.end();
51 }
52  
53 void Widget::on_pushButton_clicked()
54 {
55     x += 20;
56     if(x>width())
57     {
58         x = 0;
59     }
60     update();
61 }

QBitmap

 1 #ifndef WIDGET_H
 2 #define WIDGET_H
 3  
 4 #include <QWidget>
 5  
 6 namespace Ui {
 7 class Widget;
 8 }
 9  
10 class Widget : public QWidget
11 {
12     Q_OBJECT
13  
14 public:
15     explicit Widget(QWidget *parent = 0);
16     ~Widget();
17  
18 protected:
19     void paintEvent(QPaintEvent *event);
20  
21 private:
22     Ui::Widget *ui;
23 };
24  
25 #endif // WIDGET_H
 1 #include "widget.h"
 2 #include "ui_widget.h"
 3 #include <QPainter>
 4 #include <QBitmap>
 5 Widget::Widget(QWidget *parent) :
 6     QWidget(parent),
 7     ui(new Ui::Widget)
 8 {
 9     ui->setupUi(this);
10 }
11  
12 Widget::~Widget()
13 {
14     delete ui;
15 }
16  
17 void Widget::paintEvent(QPaintEvent *event)
18 {
19     QPainter p(this);
20     //QPixmap图片背景透明
21     p.drawPixmap(0,0,QPixmap("../Image/butterfly.png"));
22     //QBitmap
23     p.drawPixmap(200,0, QBitmap("../Image/butterfly.png"));
24     //QPixmap 图片背景白色
25     QPixmap pixmap;
26     pixmap.load("../Image/butterfly1.png");
27 }

QPixmap

 1 #ifndef WIDGET_H
 2 #define WIDGET_H
 3  
 4 #include <QWidget>
 5  
 6 namespace Ui {
 7 class Widget;
 8 }
 9  
10 class Widget : public QWidget
11 {
12     Q_OBJECT
13  
14 public:
15     explicit Widget(QWidget *parent = 0);
16     ~Widget();
17  
18 private:
19     Ui::Widget *ui;
20 };
21  
22 #endif // WIDGET_H
 1 #include "widget.h"
 2 #include "ui_widget.h"
 3 #include <QPainter>
 4 Widget::Widget(QWidget *parent) :
 5     QWidget(parent),
 6     ui(new Ui::Widget)
 7 {
 8     ui->setupUi(this);
 9     QPixmap pixmap(400, 300);
10     QPainter p(&pixmap);
11     p.fillRect(0,0,400,300,QBrush(Qt::white));
12  
13     pixmap.fill(Qt::white);
14     p.drawPixmap(0,0,80,80,QPixmap("../Image/face.png"));
15  
16     //保存图片
17     pixmap.save("../pixmap.png");
18 }
19  
20 Widget::~Widget()
21 {
22     delete ui;
23 }

QImage

 1 #include "widget.h"
 2 #include "ui_widget.h"
 3 #include <QPainter>
 4  
 5 Widget::Widget(QWidget *parent) :
 6     QWidget(parent),
 7     ui(new Ui::Widget)
 8 {
 9     ui->setupUi(this);
10  
11     QImage image(400,300,QImage::Format_ARGB32);
12     QPainter p;
13     p.begin(&image);
14  
15     p.drawLine(50,50,100,100);
16  
17     for(int i=0;i<50;i++)
18     {
19         for(int j=0;j<50;j++)
20         {
21             image.setPixel(QPoint(i,j),qRgb(0,255,0));
22  
23         }
24     }
25     p.end();
26  
27     image.save("../image.png");
28 }
29  
30 Widget::~Widget()
31 {
32     delete ui;
33 }

QPicture

 1 #include "widget.h"
 2 #include "ui_widget.h"
 3 #include <QPainter>
 4 #include <QPicture>
 5 Widget::Widget(QWidget *parent) :
 6     QWidget(parent),
 7     ui(new Ui::Widget)
 8 {
 9     ui->setupUi(this);
10     QPicture picture;
11     QPainter p;
12     p.begin(&picture);
13     p.drawLine(50,50,100,100);
14     p.end();
15  
16     picture.save("../picture.png");
17 }
18  
19 Widget::~Widget()
20 {
21     delete ui;
22 }
23  
24 void Widget::paintEvent(QPaintEvent *event)
25 {
26 //    QPicture pic;
27 //    pic.load("../picture.png");
28     QPainter p(this);
29 //    p.drawPicture(0,0,pic);
30  
31     QPixmap pixmap;
32     pixmap.load("../Image/face.png");
33     QImage tempImage = pixmap.toImage();
34     p.drawImage(0,0, tempImage);
35  
36     QImage image;
37     image.load("../Image/face.png");
38     QPixmap tempPixmap = QPixmap::fromImage(image);
39 }  

窗口透明

 1 #include "widget.h"
 2 #include "ui_widget.h"
 3 #include <QPainter>
 4 #include <QMouseEvent>
 5 #include <QDebug>
 6 Widget::Widget(QWidget *parent) :
 7     QWidget(parent),
 8     ui(new Ui::Widget)
 9 {
10     ui->setupUi(this);
11     setWindowFlags(Qt::FramelessWindowHint | windowFlags());
12  
13     setAttribute(Qt::WA_TranslucentBackground);
14 }
15  
16 Widget::~Widget()
17 {
18     delete ui;
19 }
20  
21 void Widget::paintEvent(QPaintEvent *event)
22 {
23     QPainter p(this);
24     p.drawLine(50,50,100,100);
25 }
26  
27 void Widget::mousePressEvent(QMouseEvent *event)
28 {
29     if(event->button() == Qt::RightButton)
30     {
31      close();
32      qDebug()<<"close";
33     }
34     else if(event->button() == Qt::LeftButton)
35     {
36         p = event->globalPos() - this->frameGeometry().topLeft();
37         qDebug()<<"left";
38     }
39 }
40  
41 void Widget::mouseMoveEvent(QMouseEvent *event)
42 {
43     if(event->button() & Qt::LeftButton)
44     {
45             move(event->globalPos()-p);
46     }
47 }

 

posted on 2020-08-11 15:43  一杯清酒邀明月  阅读(1313)  评论(0编辑  收藏  举报