当前位置: 首页 > news >正文

Qt 实现绘图板(支持橡皮擦与 Ctrl+Z 撤销功能)[特殊字符]

作业:

1:实现绘图的时候,颜色的随时调整

2:追加橡皮擦功能

3:配合键盘事件,实现功能

当键盘按 ctrl+z的时候,撤销最后一次绘图

头文件.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QMouseEvent>
#include<QPaintEvent>
#include<QPainter>
#include<QPen>
#include<QColor>
#include<QLine>
#include<QPoint>
#include<QPixmap>//图像容器
#include<QColorDialog>
#include<QStack>    //栈存储历史状态
#include<QKeyEvent>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void paintEvent(QPaintEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;

private slots:
void on_pushButton_clicked();

void on_pushButton_2_clicked();

void on_pushButton_3_clicked();

void on_pushButton_4_clicked();

void on_pushButton_5_clicked();

private:
    Ui::Widget *ui;

    QPoint p;
    QPainter painter;

    QPixmap *pix;
    QPen pen;
    QColor penColor;
    int penWidth;

    bool iseraser;

    QStack<QPixmap> historyStack;  // 历史画布栈
};
#endif // WIDGET_H

CPP

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    ,pix(new QPixmap(this->width(),this->height()))//实例化图纸大小及空间
    ,penColor(Qt::green)
    ,penWidth(5)
    ,iseraser(false)
{
    ui->setupUi(this);
    //填充图纸颜色
    pix->fill();//fill默认白色
}

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

void Widget::mousePressEvent(QMouseEvent *event)
{
    historyStack.push(*pix);//保存当前画布状态
    p=event->pos();
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    //让画家在图纸上画画
     QPainter painter(pix);

    //实例化一支笔
     QPen pen;//(QColor("green"));

     if(iseraser)
     {
       pen.setColor(Qt::white);
       pen.setWidth(30);
     }
     else
     {
     pen.setColor(penColor);
     pen.setWidth(penWidth);
     }

    //把笔给画家
     painter.setPen(pen);

     //让画家画线
     painter.drawLine(p,event->pos());

     //更新起始位置
     p=event->pos();

     //调用updata,触发绘制事件的发生
     this->update();

}
void Widget::paintEvent(QPaintEvent *event)
{
    //实例化画家
    QPainter painter(this);//让画家画到窗口

    //让画家画图像,把第一个画家的画 画到窗口
    painter.drawPixmap(QPoint(0,0),*pix);
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_Z)
        {
            if (!historyStack.isEmpty())
            {
                *pix = historyStack.pop();  // 取出栈顶的历史画布
                this->update();  // 重新绘制界面
            }
        }
}

void Widget::on_pushButton_clicked()
{
    //颜色
     QColor color=QColorDialog::getColor(penColor,this,"选择颜色");
     penColor=color;
     iseraser=false;

}

void Widget::on_pushButton_2_clicked()
{
    //粗细
    penWidth=5;
    iseraser=false;
}

void Widget::on_pushButton_3_clicked()
{
    //粗细10
     penWidth=10;
     iseraser=false;
}

void Widget::on_pushButton_4_clicked()
{
    //粗细20
     penWidth=20;
     iseraser=false;
}

void Widget::on_pushButton_5_clicked()
{
    //橡皮
    iseraser= true;
}

相关文章:

  • Spring Boot中对接Twilio以实现发送验证码和验证短信码
  • CAD2025电脑置要求
  • 蓝桥试题:破损的楼梯 3367
  • OSI七层网络结构和TCP/IP四层结构
  • [Windows] 多系统键鼠共享工具 轻松跨系统控制多台电脑
  • 【JavaSE-4】程序逻辑控制
  • SpringMVC请求处理流程:DispatcherServlet工作原理
  • 4.RabbitMQ工作模型
  • Linux中的进程间通信的方式及其使用场景
  • OneTools.online 一站式工具库已上线
  • Linux相关概念和易错知识点(33)(基于阻塞队列和环形队列的生产消费模型实现、sem的应用)
  • android亮灭屏流程分析
  • 基于 HTML、CSS 和 JavaScript 的智能九宫格图片分割系统
  • 机器学习(李宏毅)——Life-Long Learning
  • Java基础12
  • 【Python运维】深入分析Python运维工具:用psutil监控系统性能
  • 金蝶ERP星空对接流程
  • 机器学习11-经典网络解析
  • (1)udp双向通信(2)udp实现文件复制(3)udp实现聊天室
  • 解锁数据抓取新高度:Python 分布式爬虫与逆向进阶实战课
  • 嘉兴高端网站建设/广州白云区新闻头条最新消息今天
  • 钓鱼博彩网站怎么做/合肥360seo排名
  • 中小企业网站建设 网络营销/百度网站推广怎么做
  • 优秀政府门户网站/软件开发公司推荐
  • 手机网站复制功能实现/成都seo优化
  • 做网站需要多少钱 做/西安网站托管