Qt筆記——繪圖(QBitmap,QPixmap,QImage,QPicture)
阿新 • • 發佈:2018-04-06
背景 rgb blog 保存圖片 png ots mage 行修改 div
- 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); } }
Qt筆記——繪圖(QBitmap,QPixmap,QImage,QPicture)