QGraphicsView的paintEvent雙緩存繪畫

来源:http://www.cnblogs.com/Jace-Lee/archive/2016/12/17/6192573.html
-Advertisement-
Play Games

MyGraphicsView.h MyGraphicsView.cpp 三種繪製方法效果圖: ...


 

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

 

三種繪製方法效果圖:

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ...
  • 模板方法: 1.layout 2.partial 3.block ...
  • Scala開發環境搭建 先講幾點我學習scala的目的或者原因吧: Scala的開發環境遠比我想的要好搭建的多。下麵就不說太多的廢話了。 一、安裝JDK。 由於Scala本省也是泡在JVM上的一種編程語言,所以要按裝Scala編譯器時,要先安裝JDK,關於JDK的安裝這裡就不在贅述了,我以前也寫過一 ...
  • JavaMail,顧名思義,提供給開發者處理電子郵件相關的編程介面。它是Sun發佈的用來處理email的API。它可以方便地執行一些常用的郵件傳輸。我們可以基於JavaMail開發出類似於Microsoft Outlook的應用程式。 JavaMail包中用於處理電子郵件的核心類是:Session, ...
  • 在寫代碼時發現php foreach引用賦值會導致意外的行為。 代碼示例: <?php $arr = array('a','b','c'); foreach($arr as $k=>&$v) { } print_r($arr); foreach($arr as $k=>$v) { } print_r ...
  • chain 總結:lodash是一個能夠幫助我們更方便的對數組進行各種操作的javascript工具庫 文檔:http://lodashjs.com/docs/#_findindexarray-predicate_identity-thisarg ...
  • ...
  • <mvc:annotation-driven/> 這個便簽會註冊2個自定義攔截器,所以導致請求過來就會自己去走註冊的這2個攔截器和定義的一堆bean 但是這個便簽是必須得定義的 直接貼代碼吧 通過自定義攔截器來過濾掉靜態資源 這個問題困擾了我2天,整整熬了2天,翻遍了博客員和百度,各種瞎扯淡,也反思 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...