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

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

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

Qt笔记——绘图(QBitmap,QPixmap,QImage,QPicture)_第1张图片

  • 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

 

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

#include

#include

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

 

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

#include

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

 

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

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

 

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

#include

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

#include

#include

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

    }

}

你可能感兴趣的:(qt)