QGraphicsView的paintEvent双缓存绘画
2018-06-17 23:29:22来源:未知 阅读 ()
MyGraphicsView.h
1 #ifndef MYGRAPHICSVIEW_H 2 #define MYGRAPHICSVIEW_H 3 4 #include <QGraphicsView> 5 #include <QMouseEvent> 6 #include <QPaintEvent> 7 #include <QKeyEvent> 8 #include <QPainter> 9 #include <QPixmap> 10 #include <QDebug> 11 12 class MyGraphicsView : public QGraphicsView 13 { 14 Q_OBJECT 15 16 public: 17 MyGraphicsView(QWidget *parent); 18 ~MyGraphicsView(); 19 20 protected: 21 void mousePressEvent(QMouseEvent *event); 22 void mouseDoubleClickEvent(QMouseEvent *event); 23 void mouseMoveEvent(QMouseEvent *event); 24 void mouseReleaseEvent(QMouseEvent *event); 25 void paintEvent(QPaintEvent *event); 26 void keyPressEvent(QKeyEvent *event); 27 28 private: 29 QPixmap pix; 30 QPoint lasetPoint; 31 QPoint endPoint; 32 QPixmap tempPix; 33 bool isDrawing; 34 bool isDoubleClick; 35 }; 36 37 #endif // MYGRAPHICSVIEW_H
MyGraphicsView.cpp
#include "MyGraphicsView.h" MyGraphicsView::MyGraphicsView(QWidget *parent) : QGraphicsView(parent) { pix = QPixmap(1024, 768); pix.fill(Qt::white); isDrawing = false; isDoubleClick = false; } MyGraphicsView::~MyGraphicsView() { } void MyGraphicsView::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { lasetPoint = event->pos(); isDrawing = true; isDoubleClick = false; } QGraphicsView::mousePressEvent(event); } void MyGraphicsView::mouseDoubleClickEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { isDoubleClick = true; } QGraphicsView::mouseDoubleClickEvent(event); } void MyGraphicsView::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { if (!isDoubleClick) { endPoint = event->pos(); this->viewport()->update(); } } QGraphicsView::mouseMoveEvent(event); } void MyGraphicsView::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { if (!isDoubleClick) { endPoint = event->pos(); isDrawing = false; this->viewport()->update(); } } QGraphicsView::mouseReleaseEvent(event); } void MyGraphicsView::paintEvent(QPaintEvent *event) { /// 直接在窗体上绘图,前1次画的矩形是不能保存住的 // QPainter painter(this->viewport()); // int x, y, w, h; // x = lasetPoint.x(); // y = lasetPoint.y(); // w = endPoint.x() - x; // h = endPoint.y() - y; // painter.drawRect(x, y, w, h); /// 直接在pix上绘图,前1次前2次前3次...画的都保存住了(鼠标每移动一点就会触发paintEvent函数而画一次),所以在pix上会呈现好多矩形 // int x, y, w, h; // x = lasetPoint.x(); // y = lasetPoint.y(); // w = endPoint.x() - x; // h = endPoint.y() - y; // QPainter pp(&pix); // pp.drawRect(x, y, w, h); // QPainter painter(this->viewport()); // painter.drawPixmap(0, 0, pix); /// 双缓冲绘图,原理是在拖动过程中先把原来的图形复制到tempPix里面并在tempPix里面画,我们此时看到的就是在tempPix里的图形。只在鼠标释放的时候才在pix绘一次。 int x, y, w, h; x = lasetPoint.x(); y = lasetPoint.y(); w = endPoint.x() - x; h = endPoint.y() - y; QPainter painter(this->viewport()); if (isDrawing) { tempPix = pix; QPainter pp(&tempPix); pp.drawRect(x, y, w, h); painter.drawPixmap(0, 0, tempPix); } else { QPainter pp(&pix); pp.drawRect(x, y, w, h); painter.drawPixmap(0, 0, pix); } QGraphicsView::paintEvent(event); } void MyGraphicsView::keyPressEvent(QKeyEvent *event) { QGraphicsView::keyPressEvent(event); }
三种绘制方法效果图:
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 使用Spring Ehcache二级缓存优化查询性能 2019-08-16
- 利用ZYNQ SOC快速打开算法验证通路(3)——PS端DMA缓存数据 2018-12-04
- ASP.NET 中HttpRuntime.Cache缓存数据 2018-06-27
- 深究标准IO的缓存 2018-06-18
- 深入了解浏览器缓存原理 2018-06-18
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash