资源简介

使用Qt5.5开发的简易画图程序,支持撤销和重做功能,使用双缓冲技术,可以导出成图片。 供交流学习,本人技术简陋,希望能够对大家Qt的学习有所帮助。 注意:打开软件后请点击“新建”或“打开”开始使用。

资源截图

代码片段和文件信息

#include “icanvas.h“
#include 
#include ylePainter>
#include 
#include 
#include 
#include 

ICanvas::ICanvas(QWidget *parent) : QWidget(parent)
{
    this->setMouseTracking(true);
    this->setWindowIcon(QIcon(“:/images/canvas.ico“));

    this->undoImages = new ImageStack(50);
    this->redoImages = new ImageStack(50);
    this->brush = ICanvas::iPencil;
    this->foregroundColor = Qt::black;
    this->backgroundColor = Qt::white;
    this->isMousePressed = false;
    this->setCursor(Qt::CrossCursor);
    this->penWidth = 1;
    this->eraserWidth = 5;
    /*
    this->direction = new D_Offset[8];
    this->direction[0] = D_Offset(-1 0);
    this->direction[1] = D_Offset(-1 1);
    this->direction[2] = D_Offset(0 1);
    this->direction[3] = D_Offset(1 1);
    this->direction[4] = D_Offset(1 0);
    this->direction[5] = D_Offset(1 -1);
    this->direction[6] = D_Offset(0 -1);
    this->direction[7] = D_Offset(-1 -1);
    */
    this->direction = new D_Offset[4];
    this->direction[0] = D_Offset(-1 0);
    this->direction[1] = D_Offset(0 1);
    this->direction[2] = D_Offset(1 0);
    this->direction[3] = D_Offset(0 -1);
}

ICanvas::~ICanvas()
{
    delete [] direction;
}

void ICanvas::initCanvas()
{
    this->undoImages->clear();
    this->redoImages->clear();

    // 初始情况下压一张空图片入栈
    curPixmap = QPixmap(size());
    curPixmap.fill(this->backgroundColor);
    curImage = curPixmap.toImage();
    undoImages->push(curImage);
    this->update();
}

void ICanvas::refreshPixmap()
{
    if(undoImages->getSize() > 0)
    {
        curPixmap = QPixmap::fromImage(curImage);
    }
    else
    {
        curPixmap = QPixmap(size());
        curPixmap.fill(this->backgroundColor);
    }

    if(this->brush == ICanvas::iPencil)
    {
        // 铅笔工具
        QPainter painter(&curPixmap); // 画布
        QPen pen;  // 画笔
        pen.setColor(this->foregroundColor); // 设置为前景色
        pen.setWidth(this->penWidth);
        painter.setPen(pen);
        for(int i = 1; i < trackPoints.size(); i++)
        {
            painter.drawLine(trackPoints[i - 1] trackPoints[i]);
        }
    }
    else if(this->brush == ICanvas::iStraight)
    {
        // 直线工具
        QPainter painter(&curPixmap); // 画布
        QPen pen;  // 画笔
        pen.setColor(this->foregroundColor); // 设置为前景色
        pen.setWidth(this->penWidth);
        painter.setPen(pen);
        painter.drawLine(trackPoints[0] trackPoints[trackPoints.size() - 1]);
    }
    else if(this->brush == ICanvas::iPolyline)
    {
        // 折线工具
        QPainter painter(&curPixmap); // 画布
        QPen pen;  // 画笔
        pen.setColor(this->foregroundColor); // 设置为前景色
        pen.setWidth(this->penWidth);
        painter.setPen(pen);
        painter.drawLine(trackPoints[0] trackPoints[trackPoints.size() - 1]);
    }
    else if(this->brush == ICanvas::i

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-09-13 10:32  iPaint4Qt5.5\
     目录           0  2015-09-13 10:32  iPaint4Qt5.5\IPaint\
     文件       11590  2015-09-08 18:50  iPaint4Qt5.5\IPaint\icanvas.cpp
     文件        2638  2015-09-13 10:27  iPaint4Qt5.5\IPaint\icanvas.h
     目录           0  2015-09-13 10:32  iPaint4Qt5.5\IPaint\images\
     文件         875  2015-09-08 13:54  iPaint4Qt5.5\IPaint\imagestack.cpp
     文件         882  2015-09-08 13:53  iPaint4Qt5.5\IPaint\imagestack.h
     文件         200  2009-10-21 01:47  iPaint4Qt5.5\IPaint\images\background.png
     文件      325638  2009-04-29 04:43  iPaint4Qt5.5\IPaint\images\canvas.ico
     文件         603  2009-06-02 02:54  iPaint4Qt5.5\IPaint\images\circle.png
     文件         563  2009-06-02 02:53  iPaint4Qt5.5\IPaint\images\ellipse.png
     文件         788  2009-06-02 02:53  iPaint4Qt5.5\IPaint\images\eraser.png
     文件         702  2009-01-30 22:57  iPaint4Qt5.5\IPaint\images\exit.png
     文件         732  2009-09-09 01:43  iPaint4Qt5.5\IPaint\images\fill.png
     文件         207  2009-10-21 01:44  iPaint4Qt5.5\IPaint\images\foreground.png
     文件         336  2009-09-10 00:21  iPaint4Qt5.5\IPaint\images\line.png
     文件         514  2009-04-22 03:46  iPaint4Qt5.5\IPaint\images\new.png
     文件         652  2009-04-22 03:47  iPaint4Qt5.5\IPaint\images\open.png
     文件      325638  2009-04-29 05:14  iPaint4Qt5.5\IPaint\images\paint.ico
     文件         940  2009-05-21 00:23  iPaint4Qt5.5\IPaint\images\paint.png
     文件         399  2009-07-27 10:22  iPaint4Qt5.5\IPaint\images\pencil.png
     文件         529  2009-06-02 02:53  iPaint4Qt5.5\IPaint\images\polygon.png
     文件         524  2015-06-30 04:53  iPaint4Qt5.5\IPaint\images\qt.png
     文件         351  2009-06-02 02:53  iPaint4Qt5.5\IPaint\images\rectangle.png
     文件         732  2009-04-22 04:21  iPaint4Qt5.5\IPaint\images\redo.png
     文件         554  2009-04-22 04:03  iPaint4Qt5.5\IPaint\images\save.png
     文件         768  2008-04-15 04:17  iPaint4Qt5.5\IPaint\images\saveAs.png
     文件         720  2009-04-22 04:20  iPaint4Qt5.5\IPaint\images\undo.png
     文件       17934  2015-09-13 10:26  iPaint4Qt5.5\IPaint\ipaint.cpp
     文件        2291  2015-09-13 10:29  iPaint4Qt5.5\IPaint\ipaint.h
     文件         462  2015-09-13 10:19  iPaint4Qt5.5\IPaint\IPaint.pro
............此处省略4个文件信息

评论

共有 条评论