Bootstrap

QT撤销——————

#ifndef WIDGET_H
#define WIDGET_H

#include <QDebug>
#include <QWidget>
#include <QPainter>
#include <QPen>
#include <QLine>
#include <QVector>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QObject>
#include <QColorDialog>
#include <QFileDialog>
#include <QImageReader>
#include <QMenuBar>
#include <QKeyEvent>


QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class myLine{
    QLine line;
    QPen pen;
public:
    myLine(){}
    myLine(const QLine& line , const QPen& pen):line(line),pen(pen){}
    QLine getline(){return line;}//轨迹
    QPen getpen(){return pen;}//笔
};
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
protected:
    virtual void paintEvent(QPaintEvent *event) override;
    virtual void mouseMoveEvent(QMouseEvent *event) override;
    virtual void mousePressEvent(QMouseEvent *event) override;
    virtual void mouseReleaseEvent(QMouseEvent *event) override;
    virtual void keyPressEvent(QKeyEvent *event) override;

private slots:
    void on_verticalSlider_sliderMoved(int position);

    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_saveAction_Clicked();

    void on_loadAction_Clicked();
private:
    Ui::Widget *ui;
    QPainter painter;
    QPen pen;
    QPoint start;
    QPoint end;
    QVector<myLine> lines;
    QVector<int> lines_endI;
    int count;
    QMenuBar* bar;

    QAction* save_action;
    QAction* load_action;

    QPixmap load_pic;

};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    count = 0;//初始化计量
    ui->verticalSlider->setRange(0,10);
    bar = new QMenuBar(this);
    QMenu* m1 = bar->addMenu("FILE");
    save_action = m1->addAction("save");
    load_action = m1->addAction("load");
    QObject::connect(save_action , &QAction::triggered ,this ,&Widget::on_saveAction_Clicked);
    QObject::connect(load_action , &QAction::triggered ,this ,&Widget::on_loadAction_Clicked);
    bar->addMenu("MUNE");

}

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

void Widget::paintEvent(QPaintEvent *event)
{
    painter.begin(this);

    painter.drawPixmap(this->rect(), load_pic);//将load_pic绘制在当前窗口矩阵里
    //每一个ele是一个myLine对象
    for(auto ele:lines){
        painter.setPen(ele.getpen());
        painter.drawLine(ele.getline());
    }
    painter.end();
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    count++;
     qDebug() << count;
    end = event->pos();//移动后产生新的终点坐标
    //记录轨迹
    QLine l(end , start);//轨迹
    myLine line(l,pen);//将二者绑定
    lines << line;//放入容器
    start = end;//更新起始坐标
    update();//触发绘图事件
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    start = event->pos();
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    end = event->pos();
    lines_endI << count;//每条线长
    qDebug() << "count" << count;

    count = 0;
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    int key =event->key();//键入
    Qt::KeyboardModifiers mode = event->modifiers();//组合键
    if(mode == Qt::ControlModifier && key == Qt::Key_Z ){
        //线长end_i
        int end_i = lines_endI.size()-1;
        if(end_i<=-1){return;}
        int end_count = lines_endI[end_i];//最末线长
        //线尾坐标i
        int i = lines.size() -1;
        if(i<=-1){return;}


        lines.remove(i-end_count+1 , end_count);
        lines_endI.remove(end_i , 1);
        update();
    }
}


void Widget::on_verticalSlider_sliderMoved(int position)
{
    pen.setWidth(position);
}

void Widget::on_pushButton_clicked()
{
    QColor color = QColorDialog::getColor(Qt::white , this ,"选择颜色" ,QColorDialog::ShowAlphaChannel);
    pen.setColor(color);
}

void Widget::on_pushButton_2_clicked()
{
    QPalette pe = this->palette();
    QColor color = pe.color(QPalette::Window);
    pen.setColor(color);
}

void Widget::on_saveAction_Clicked()
{
    qDebug() <<"save";
    QPixmap pic = this->grab();//获取当前像素信息
    QImage image = pic.toImage();
    QString path = QFileDialog::getSaveFileName(this , "保存文件" ,"F:/QT_Picture" , "image(*.jpg *.jpeg *.png *.bmp);;c++(*.c *.cpp)");
    image.save(path,"jpg");
}

void Widget::on_loadAction_Clicked()
{
    QString path  = QFileDialog::getOpenFileName(this ,"加载文件" ,"F:/QT_Picture"  , "image(*.jpg *.jpeg *.png *.bmp);;c++(*.c *.cpp)");
    QImageReader reader(path , "jpg");//jpg读取path文件,并以读取的数据构建一个reader对象
    QImage image  = reader.read();
    load_pic = QPixmap::fromImage(image);
    update();
}

 

;