(十一)QPainter繪圖, QPixmap,QImage,QPicture,QBitmap


 

 

#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QFont>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    x = 20;
    ui->setupUi(this);

    connect(ui->move, &QPushButton::clicked, this, [=]()
    {
       // 刷新窗口
        update();   // 系統調用paintEvent 函數
    });
}

Widget::~Widget()
{
    delete ui;
}


void Widget::paintEvent(QPaintEvent *)
{
    // 創建畫家類對象
    QPainter p(this);   // 指定繪圖設備

#if 0
    // 創建新畫筆 -- 輪廓
    QPen pen;
    pen.setColor(/*Qt::green*/QColor(0, 255, 0));
    pen.setWidth(10);   // 像素
    pen.setStyle(Qt::DotLine);

    // 閉合區域使用畫刷
    QBrush brush(QPixmap(":/Image/face.png"));
    p.setBrush(brush);

    // 將新畫筆設置給畫家類
    p.setPen(pen);
    // 畫背景圖
    p.drawPixmap(0, 0, QPixmap(":/Image/xks.png"));

    // 畫直線
    p.drawLine(QPoint(100, 100), QPoint(300, 500));

    // 畫橢圓
    p.drawEllipse(QPoint(200, 200), 100, 50);

    // 畫矩形
    p.drawRect(400, 200, 200, 200);

    // 寫字
    QFont font("華文彩雲", 48, 75, true);
    p.setFont(font);
    p.drawText(100, 400, "我是中國人, 我愛我的祖國!!!");

    int width = this->width();
    int heght = this->height();
#endif
    // 提供笑臉
    x += 5;
    if(x > this->width())
    {
        x = 20;
    }
    p.drawPixmap(x, 100, QPixmap(":/Image/sunny.png"));

}

 

#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:
    /*
     *  1. 回調函數
     *  2. 此函數不需要用戶與調用, 再刷新的時候會自定調用
     *      1. 窗口顯示的時候
     *      2. 最大化, 最小化
     *      3. 窗口被這遮擋, 重新顯示的時候
     *      4. 用戶強制刷新的時候
     *      5. ...........
     *  3. 如果想使用畫家類在窗口中畫圖, 操作必須在paintEvent函數中完成
    */
    void paintEvent(QPaintEvent *);

private:
    Ui::Widget *ui;
    int x;
};

#endif // WIDGET_H

 QPixmap,QImage,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);

    // 在QPixmap中畫圖
    QPixmap pix(300, 300);  // 紙的大小
    pix.fill(Qt::red);
    QPainter p(&pix);
    p.setPen(QPen(Qt::green, 10));
    p.drawRect(10, 10, 280, 280);
    p.drawEllipse(150, 150, 50, 50);
    pix.save("D:\\mypixmap.png");

    // 指定繪圖設備 1. 構造函數中(參數是繪圖設備)
    // 2. begin(參數是繪圖設備)
     //    end();
     // 在QImage中畫圖
    QImage img(300, 300, QImage::Format_RGB32);  // 紙的大小
    img.fill(Qt::red);
    p.begin(&img);
    p.setPen(QPen(Qt::green, 10));
    p.drawRect(10, 10, 280, 280);
    p.drawEllipse(150, 150, 50, 50);
    p.end();
    img.save("D:\\myImage.png");

    // 在QPicture中畫圖
    // 1. 保存的是繪圖步驟 -- 畫家類
    // 2. 不是圖片, 二進制文件(save保存生成的文件)
    // 3. 不依賴平台
   QPicture pic;  // 紙的大小
   p.begin(&pic);
   p.setPen(QPen(Qt::green, 10));
   p.drawRect(10, 10, 280, 280);
   p.drawEllipse(150, 150, 50, 50);
   p.end();
   pic.save("D:\\mypic.aaa");
}

// QWidget
// QPixmap QImage QPicture QBitmap(黑白圖片)
// QBitmap 父類 QPixmap
// QPixmap -- 圖片類, 主要用來顯示, 它針對於顯示器顯示做了特殊優化, 依賴於平台的, 只能在主線程中使用(UI線程)
// QIamge -- 圖片類 , 不依賴有平台, (圖片傳輸 , 可以在多線程中對其進行操作)
Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    QPicture pic;
    pic.load("D:\\mypic.png");
    p.drawPicture(100, 100, pic);
}

 

QPixmap:
    p.load("路徑");———加載圖片
    p.drawPixmap(b);
    a.專門為圖像在屏幕上的顯示做了優化,依賴於平台
    b.主要應用於平台上的圖形顯示,在不同的平台上擁有相同是顯示效果
QBitmap b;--
    p.drawPixmap(b)
    是QPixmap的一個子類,只顯示黑白色
QImage
    a.使用獨立於硬件的繪制系統,專門為圖像的像素級訪問做了優化
    b.可以在多線程中使用
    c.可以修改圖片中的任意一個像素值
QPicture -- 二進制文件
    a.記錄和重現QPainter 的各種命令
    b.與平台無關
    
QPixmap 和 QImage 的直接相互轉換
    1.QPixmap-->QImage:QPixmap::toImage()
    2.QImage-->QPixmap:QPixmap::fromImage() [static]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM