• Qt之简单绘图实现


    效果图:

                                                

     可以实现打开图片,在打开的图片上画图,可以保存图片,以及橡皮擦,画笔调大调粗换色功能。

    代码:

    XImage.h:

    #ifndef __XIMAGE__H__
    #define __XIMAGE__H__
    #include "qwidget.h"
    #include<QPaintEvent>
    #include<QMouseEvent>
    #include<QColor>
    #include<Qpen>
    class XImage :
        public QWidget
    {
        Q_OBJECT
    public:
        XImage(QWidget* parent=NULL);
        ~XImage();
        void paintEvent(QPaintEvent* ev) override;
        void mouseMoveEvent(QMouseEvent* ev) override;
        void mouseReleaseEvent(QMouseEvent* ev) override;
        public slots:
        void Open();
        void SetPen(int size,QColor col);
        void SetErase(int size);
        void Save();
    private:
        QImage src;
        QPoint mpos;
        QPen pen;
        QImage img;
    };
    #endif

    XImage.cpp:

    #include "XImage.h"
    #include<QFileDialog>
    #include "ui_XPS.h"
    #include<QPainter>
    XImage::XImage(QWidget* parent):QWidget(parent){}
    
    void XImage::Open()
    {
        QString filename = QFileDialog::getOpenFileName(this, QStringLiteral("打开图片"),
            "",QStringLiteral("支持格式(*.png *.jpg)"));
        if (filename.isNull())return;
        if (!src.load(filename))return;
        mpos = QPoint();
        img = src.copy();
        update();
    }
    
    void XImage::mouseMoveEvent(QMouseEvent* ev)
    {
        QPainter painter(&img);
        painter.setPen(pen);
        if (mpos.isNull()) mpos = ev->pos();
        painter.drawLine(QLine(mpos,ev->pos()));
        mpos = ev->pos();
        update();
    }
    
    
    void XImage::SetPen(int size, QColor col)
    {
        pen.setWidth(size);
        pen.setColor(col);
        pen.setCapStyle(Qt::RoundCap);
        pen.setJoinStyle(Qt::RoundJoin);
    }
    
    void XImage::mouseReleaseEvent(QMouseEvent* ev)
    {
        mpos = QPoint();
    }
    
    void XImage::paintEvent(QPaintEvent* ev)
    {
        QPainter painter(this);
        painter.drawImage(0,0,img);    
    }
    
    void XImage::SetErase(int size)
    {
        pen.setWidth(size);
        pen.setBrush(src);
    }
    
    void XImage::Save()
    {
        QString filename = QFileDialog::getSaveFileName(this, QStringLiteral("打开图片"),
            "", QStringLiteral("支持格式(*.png *.jpg)"));
        if (filename.isNull())return;
        if (!src.save(filename))return;
    }
    
    
    
    
    XImage::~XImage(){}

    XSP.h:

    #ifndef __XPS__H__
    #define __XPS__H__
    
    #include <QtWidgets/QMainWindow>
    #include "ui_XPS.h"
    #include<QColor>
    class XPS : public QMainWindow
    {
        Q_OBJECT
    
    public:
        XPS(QWidget *parent = Q_NULLPTR);
    public slots:
        void SetPen();
        void SetColor();
        void SetErase();
        void Save();
    private:
        Ui::XPSClass ui;
        QColor col;
    };
    #endif

    XSH.cpp:

    #include "XPS.h"
    #include<QColorDialog>
    #include<QButtonGroup>
    XPS::XPS(QWidget *parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
        
        connect(ui.openaction,SIGNAL(triggered()),ui.Image,SLOT(Open()));
        connect(ui.saveaction,SIGNAL(triggered()),ui.Image,SLOT(Save()));
        col = QColor(250,0,0);
        SetPen();
    
        QButtonGroup* group = new QButtonGroup(this);
        group->addButton(ui.penButton);
        group->addButton(ui.eraseButton);
        group->setExclusive(true);
    }
    
    void XPS::SetPen()
    {
        ui.Image->SetPen(ui.pensize->value(),col);
        ui.penButton->setChecked(true);
    }
    
    void XPS::SetColor()
    {
            col = QColorDialog::getColor(Qt::red, this);
            QString sty = QString("background-color:rgba(%1,%2,%3,%4)")
                .arg(col.red()).arg(col.green()).arg(col.blue()).arg(col.alpha());
            ui.colorButton->setStyleSheet(sty);
            SetPen();
    }
    
    void XPS::SetErase()
    {
        ui.Image->SetErase(ui.pensize->value());
        ui.eraseButton->setChecked(true);
    }
    
    void XPS::Save()
    {
        ui.Image->Save();
        ui.statusBar->showMessage(QStringLiteral("保存结束"), 5000);
    }

    main.cpp:

    #include "XPS.h"
    #include <QtWidgets/QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        XPS w;
        w.show();
        return a.exec();
    }

    ui界面部分:

     

    测试环境:vs2015

    可能出现的问题:

    要包含该目录:

  • 相关阅读:
    JSP+Ajax站点开发小知识
    JavaScript向select下拉框中加入和删除元素
    debain install scim
    Xcode 5.1.1 与 Xcode 6.0.1 共存
    Oracle集合操作函数:Union、Union All、Intersect、Minus
    8皇后-----回溯法C++编程练习
    Copy-and-swap
    Android System Property 解析
    Android 仿PhotoShop调色板应用(二) 透明度绘制之AlphaPatternDrawable
    Android 仿PhotoShop调色板应用(一)概述
  • 原文地址:https://www.cnblogs.com/SunShine-gzw/p/13631518.html
Copyright © 2020-2023  润新知