QT下简易画板实现

xiaoxiao2021-02-27  171

//maindow.h

#ifndef MAINWINDOW_H #define MAINWINDOW_H

#include"paintwidget.h" #include <QMainWindow> #include<QScrollArea>

#include<QComboBox>  //颜色组合框头

namespace Ui {     class MainWindow; }

class MainWindow : public QMainWindow {     Q_OBJECT

public:     explicit MainWindow(QWidget *parent = 0);     ~MainWindow();

    void doNew();  //新建文件操作     void doOpen();  //打开文件操作     bool doFileSave();  //保存文件操作     bool doFileSaveAs();  //文件另存为操作

    void creatColorComboBox(QComboBox*comboBox);  //构建颜色组合框

private:     Ui::MainWindow *ui;     PaintWidget*area;     QScrollArea*scrollArea;

    bool isSaved;  //标志文件是否保存过     QString curFile;  //保存当前文件的路径     bool maybeSave();  //是否保存文件

    bool saveFile(QString fileName);  //实现文件的存储

protected:     void changeEvent(QEvent *e);     void closeEvent(QCloseEvent*);

private slots:     void on_action_triggered();     void on_brushColorComboBox_currentIndexChanged(int index);     void on_penColorComboBox_currentIndexChanged(int index);     void on_penWidthSpinBox_valueChanged(int );     void on_penStyleComboBox_currentIndexChanged(QString );     void on_shapeComboBox_currentIndexChanged(QString );     void on_action_28_triggered();     void on_action_26_triggered();     void on_action_24_triggered();     void on_action_23_triggered();     void on_action_22_triggered();     void on_action_21_triggered();     void on_action_20_triggered();     void on_action_P_triggered();     void on_action_X_triggered();     void on_action_A_triggered();     void on_action_S_triggered();     void on_action_O_triggered();     void on_action_N_triggered(); };

#endif // MAINWINDOW_H

 

 

//paintwidget.h

 

 

#ifndef PAINTWIDGET_H #define PAINTWIDGET_H

#include <QWidget> #include<QMouseEvent> #include<QPoint>

class PaintWidget : public QWidget {     Q_OBJECT public:     //explicit PaintWidget();

    PaintWidget();     void setImageSize(int width,int height);  //声明改变画布大小函数     void setImageColor(QColor color);  //声明改变画布颜色函数

    bool isModified() const{return modified;}  //判断画布内容是否被更改过     bool saveImage(const QString&fileName,const char*fileFormat); //保存图片     bool openImage(const QString&fileName);  //打开图片

    QSize getImageSize();

    void doPrint();  //打印菜单功能函数声明

    //工具菜单功能函数声明     void zoomIn();  //放大     void zoomOut();  //缩小     void zoom_1();  //还原     void doRotate();  //旋转     void doShear();  //拉伸     void doClear();  //清空

    //画笔颜色     void setPenStyle(Qt::PenStyle style);  //设置画笔风格     void setPenWidth(int width);  //设置画笔宽度     void setPenColor(QColor color);  //设置画笔颜色     void setBrushColor(QColor color);  //设置填充颜色

    enum ShapeType  //枚举变量,几个图形的选择     {         None,         Line,         Rectangle,         Ellipse,         ERASE     };     void setShape(ShapeType shape);  //设置要绘制的图形

protected:     void paintEvent(QPaintEvent*);  //重绘事件

    void mousePressEvent(QMouseEvent*);  //鼠标按下事件     void mouseMoveEvent(QMouseEvent*);  //鼠标移动事件     void mouseReleaseEvent(QMouseEvent*);  //鼠标释放事件     void paint(QImage&theImage);

private:     QImage image;  //QImage类对象,用于在其上绘图     QRgb backColor;  //QRgb颜色对象,存储image的背景色

    QPoint lastPoint,endPoint;  //定义两个坐标对象存放鼠标指针的前后两个坐标

    bool modified;  //标志画布是否被更改过

    //工具变量声明     qreal scale;  //缩放量     int angle;  //角度     qreal shear;  //拉伸量

    //画笔填充变量     QColor penColor;  //画笔颜色     QColor brushColor;  //填充颜色     int penWidth;  //画笔宽度     Qt::PenStyle penStyle;  //画笔风格     ShapeType curShape;  //当前图形

    QImage tempImage;  //临时绘图区     bool isDrawing;  //是否在绘制特殊图形

signals:

public slots:

};

#endif // PAINTWIDGET_H

 

 

//donewdialog.h

 

 

#ifndef DONEWDIALOG_H #define DONEWDIALOG_H

#include <QDialog>

namespace Ui {     class DoNewDialog; }

class DoNewDialog : public QDialog {     Q_OBJECT

public:     explicit DoNewDialog(QWidget *parent = 0);     ~DoNewDialog();     int getWidth();  //用于返回画布的宽     int getHeight();  //用于返回画笔的高     QColor getBackColor();  //用于返回画布的背景色

protected:     void changeEvent(QEvent *e);

private:     Ui::DoNewDialog *ui;     QColor backColor;  //用于保存画布的背景色

private slots:     void on_toolButton_clicked(); };

#endif // DONEWDIALOG_H

//mainwindow.cpp

 

#include <QtGui/QApplication> #include "mainwindow.h" #include <QTextCodec>

int main(int argc, char *argv[]) {     QApplication a(argc, argv);     QTextCodec::setCodecForTr(QTextCodec::codecForLocale());     MainWindow w;     w.show();

    return a.exec(); }

 

//paintwidget.cpp

 

#include "paintwidget.h" #include<QPainter>

#include<QPrintDialog> #include<QPrinter>

 

/*构造函数初始化对象*/ PaintWidget::PaintWidget()

{     image=QImage(400,300,QImage::Format_RGB32);  //画布的初始化大小设为400*300,使用32位颜色     backColor=qRgb(255,255,255);  //画布初始化背景色使用白色     image.fill(backColor);

    modified=false;     //modified=true;

    scale=1;     angle=0;     shear=0;

    penColor=Qt::black;     brushColor=Qt::black;     penWidth=1;     penStyle=Qt::SolidLine;     curShape=None;

    isDrawing=false;

}

/*定义重绘函数*/ void PaintWidget::paintEvent(QPaintEvent*) {     QPainter painter(this);     //painter.drawImage(0,0,image);

    //更改重绘事件函数     painter.scale(scale,scale);     if(isDrawing)  //如果正在绘制特殊图形,则显示临时绘图区上的内容     {         painter.drawImage(0,0,tempImage);     }     else     {         if(angle)         {             QImage copyImage=image;  //新建临时的copyImage,利用它进行旋转操作             QPainter pp(©Image);             QPointF center(copyImage.width()/2.0,copyImage.height()/2.0);             pp.translate(center);             pp.rotate(angle);             pp.translate(-center);             pp.drawImage(0,0,image);             image=copyImage;  //只会复制图片上的内容,不会复制坐标系统             angle=0;  //完成旋转后将角度值重新设为0

            /*QPointF center(image.width()/2.0,image.height()/2.0);             painter.translate(center);             painter.rotate(angle);             painter.translate(-center);*/

        }         if(shear)         {             //painter.shear(shear,shear);             QImage copyImage=image;             QPainter pp(©Image);             pp.shear(shear,shear);             pp.drawImage(0,0,image);             image=copyImage;             shear=0;         }         painter.drawImage(0,0,image);

    }

}

QSize PaintWidget::getImageSize() {     return image.size()*scale; }

/*定义鼠标按下、移动、释放函数*/ void PaintWidget::mousePressEvent(QMouseEvent*event) {     if(event->button()==Qt::LeftButton)  //当鼠标左键按下     {         lastPoint=event->pos();  //获得鼠标指针当前坐标作为起始坐标         isDrawing=true;     } }

void PaintWidget::mouseMoveEvent(QMouseEvent*event) {     if(event->buttons()&Qt::LeftButton)  //如果鼠标左键按着的同时移动鼠标     {         endPoint=event->pos();  //获得鼠标指针当前坐标作为终止坐标

        if(curShape==None || curShape==ERASE)  //如果不进行特殊图形绘制,则直接在image上绘制         {             isDrawing=false;             paint(image);  //绘制图形

        }         else  //如果绘制特殊图形,则再临时绘图区tempImage上绘制         {             tempImage=image;  //每次绘制tempImage前用上一次image中的图片对其进行填充             paint(tempImage);         }     } }

void PaintWidget::mouseReleaseEvent(QMouseEvent*event) {     if(event->button()==Qt::LeftButton)  //如果鼠标左键释放     {         endPoint=event->pos();  //获得鼠标指针当前坐标作为终止坐标         isDrawing=false;         paint(image);  //绘制图形

    } }

void PaintWidget::paint(QImage&theImage) {     QPainter pp(&theImage);  //在theImage上绘图

    /*     pp.drawLine(lastPoint,endPoint);  //由起始坐标和终止坐标绘制直线     update();  //进行更新界面显示,可引起窗口重绘事件,重绘窗口     */

//    QPainter pp(&theImage);  //在zheImage上绘图     QPen pen=QPen();     pen.setColor(penColor);     pen.setStyle(penStyle);     pen.setWidth(penWidth);     QBrush brush=QBrush(brushColor);     pp.setPen(pen);     pp.setBrush(brush);     QRect rect;

    int x,y,w,h;     x=lastPoint.x()/scale;     y=lastPoint.y()/scale;     w=(endPoint.x()-x)/scale;     h=(endPoint.y()-y)/scale;

    switch(curShape)     {     case None:  //不绘制特殊图形         {             pp.drawLine(lastPoint/scale,endPoint/scale);  //由起始坐标和终止坐标绘制直线             lastPoint=endPoint;  //让终止坐标变为起始坐标             break;         }     case Line:  //绘制直线         {             pp.drawLine(lastPoint/scale,endPoint/scale);             break;         }     case Rectangle:  //绘制矩形         {             pp.drawRect(x,y,w,h);             break;         }     case Ellipse:  //绘制椭圆         {             pp.drawEllipse(x,y,w,h);             break;         }     case ERASE:  //橡皮擦         {             rect=QRect(lastPoint,QPoint(lastPoint.x()+10,lastPoint.y()+10));  //橡皮擦大小             pp.eraseRect(rect);             lastPoint=endPoint;             break;         }     }     update();  //进行更新界面显示,可引起窗口重绘事件,重绘窗口     modified=true; }

void PaintWidget::setImageSize(int width,int height) {     QImage newImage(width,height,QImage::Format_RGB32);     image=newImage;     update(); }

 

void PaintWidget::setImageColor(QColor color) {     backColor=color.rgb();  //因为image的背景色要用QRgb类型的颜色,所以这里进行了转换     image.fill(backColor);     update(); }

 

bool PaintWidget::saveImage(const QString&fileName,const char*fileFormat) {     QImage visibleImage=image;

    if(visibleImage.save(fileName,fileFormat))  //实现了文件存储     {         modified=false;         return true;     }     else     {         return false;     } }

bool PaintWidget::openImage(const QString&fileName) {     QImage loadedImage;     if(!loadedImage.load(fileName))         return false;

    QSize newSize=loadedImage.size();     setImageSize(newSize.width(),newSize.height());     image=loadedImage;

    modified=false;     update();     return true; }

 

void PaintWidget::doPrint() {     QPrinter printer(QPrinter::HighResolution);

    QPrintDialog*printDialog=new QPrintDialog(&printer,this);     if(printDialog->exec()==QDialog::Accepted)     {         QPainter painter(&printer);         QRect rect=painter.viewport();         QSize size=image.size();         size.scale(rect.size(),Qt::KeepAspectRatio);         painter.setViewport(rect.x(),rect.y(),size.width(),size.height());         painter.setWindow(image.rect());         painter.drawImage(0,0,image);

    } }

//编辑菜单功能函数定义

void PaintWidget::zoomIn() {     scale*=1.2;     update(); }

void PaintWidget::zoomOut() {     scale/=1.2;     update(); }

void PaintWidget::zoom_1() {     scale=1;     update(); }

void PaintWidget::doRotate() {     angle+=90;     update(); }

void PaintWidget::doShear() {     shear=0.2;     update(); }

void PaintWidget::doClear() {     image.fill(backColor);  //用现在的画布背景色进行填充     update(); }

//画笔填充函数定义 void PaintWidget::setPenStyle(Qt::PenStyle style) {     penStyle=style; }

void PaintWidget::setPenWidth(int width) {     penWidth=width; }

void PaintWidget::setPenColor(QColor color) {     penColor=color; }

void PaintWidget::setBrushColor(QColor color) {     brushColor=color; }

void PaintWidget::setShape(ShapeType shape) {     curShape=shape; }

 

 

//donewdialog.cpp

 

 

#include "donewdialog.h" #include "ui_donewdialog.h"

#include<QColorDialog> #include<QDebug> DoNewDialog::DoNewDialog(QWidget *parent) :     QDialog(parent),     ui(new Ui::DoNewDialog) {     ui->setupUi(this);     //初始化     backColor=Qt::white; }

DoNewDialog::~DoNewDialog() {     delete ui; }

//定义 void DoNewDialog::changeEvent(QEvent *e) {     QDialog::changeEvent(e);     switch(e->type())     {     case QEvent::LanguageChange:         ui->retranslateUi(this);         break;     default:         break;     } }

int DoNewDialog::getWidth() {     return ui->widthSpinBox->text().toInt(); }

int DoNewDialog::getHeight() {     return ui->heightSpinBox->text().toInt(); }

QColor DoNewDialog::getBackColor() {     return backColor; }

 

 

void DoNewDialog::on_toolButton_clicked() {     QColor newColor=QColorDialog::getColor();  //从颜色对话框获得颜色     if(newColor.isValid())  //如果得到的是可用的颜色     {         backColor=newColor;

        QPalette palette=ui->textBrowser->palette();  //显示这个颜色         palette.setColor(QPalette::Base,backColor);         ui->textBrowser->setPalette(palette);         update();     }

}

FROM:http://lizhigg.love.blog.163.com/blog/static/62611781201222105550184/

http://blog.csdn.net/imxiangzi/article/details/49310573

转载请注明原文地址: https://www.6miu.com/read-14730.html

最新回复(0)