Qt之旅---10 雙緩衝繪圖

1 基本知識git

      但咱們使用painter進行快速繪圖時會看到相似圖下 的狀況ui

 雙緩衝繪圖技術 的原理 用兩個畫布進行繪圖,一個用於顯示,一個用於繪製,也就是將圖畫以後再顯示this

就能夠避免上面的狀況。code

2 源碼get

#include "mainwindow.h"
#include "ui_mainwindow.h"源碼

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    isDrawing=false;
    //重設窗口大小
    resize(600,500);
    //設置畫布大小
     pix=QPixmap(200,200);
pix.fill(Qt::white);qt

    isDrawing=false;
}it

MainWindow::~MainWindow()
{
    delete ui;
}event

void  MainWindow::paintEvent(QPaintEvent *){
int x,y,h,w;
  x=lastpoint.x();
  y=lastpoint.y();
  w=endtpoint.x()-x;
  h=endtpoint.y()-y;ast

  //繪圖設備
  QPainter pait(this);
  if(isDrawing)
  {
      //將pix 複製到 tmpPix(補助畫布)中 保存讓之前 的畫不消失
      tmpPix=pix;
      QPainter pp(&tmpPix);
      pp.drawRect(x,y,w,h);
      pait.drawPixmap(0,0,tmpPix);
  }
  else{

      QPainter  pp(&pix);
      pp.drawRect(x,y,w,h);
      pait.drawPixmap(0,0,pix);
  }

}

void  MainWindow::mousePressEvent(QMouseEvent *event){
    if(event->button()==Qt::LeftButton){
        lastpoint==event->pos();
        isDrawing=true;
        update();
    }
}

void  MainWindow::mouseReleaseEvent(QMouseEvent *event){
    if(event->button()==Qt::LeftButton)
    {


        endtpoint=event->pos();
        isDrawing=false;
        update();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{

    if(event->buttons()==Qt::LeftButton)
    {

        endtpoint=event->pos();
        update();
    }
}
 

Demo22 地址:https://gitee.com/codemaner/qt_learning_record/tree/master

相關文章
相關標籤/搜索