Qt笔记——绘图(QBitmap,QPixmap,QImage,QPicture)

Qt笔记——绘图(QBitmap,QPixmap,QImage,QPicture)

https://www.cnblogs.com/dalanjing/p/8724876.html

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

QPainter

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

#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

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

#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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

#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

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

#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

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#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

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

#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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

#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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

#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);

}  

窗口透明

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

#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);

    }

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值