zoukankan      html  css  js  c++  java
  • 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

    可能出现的问题:

    要包含该目录:

  • 相关阅读:
    试试Linux下的ip命令,ifconfig已经过时了
    VirtualBox中Linux虚拟机与主机共享文件夹
    VirtualBox内刚刚安装完CentOS6.9和7系统,无法调整屏幕的分辨率,也无法设置共享文件夹。解决的方法就是安装VirtualBox客户端增强包。
    Ftp、Ftps与Sftp之间的区别
    Linux 桌面的 Dock 类程序
    Dubbo服务超时
    Dubbo启动时检查
    Dubbo配置参数的优先级
    Dubbo监控中心
    SpringBoot集成监控管理
  • 原文地址:https://www.cnblogs.com/SunShine-gzw/p/13631518.html
Copyright © 2011-2022 走看看