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

QT-绘画事件

实现颜色的随时调整,追加橡皮擦功能

widget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QColor>
#include <QPoint>
#include <QVector>
#include <QMouseEvent>
#include <QPainter>
#include <QColorDialog>// 自定义 Line 类,存储线段的起点、终点、颜色和宽度
class Line {
public:Line(const QPoint& start, const QPoint& end, const QColor& color, int width): start(start), end(end), color(color), width(width) {}QPoint start;QPoint end;QColor color;int width;
};QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();protected:void paintEvent(QPaintEvent *event) override; // 重写绘图事件void mouseMoveEvent(QMouseEvent *event) override; // 重写鼠标移动事件void mousePressEvent(QMouseEvent *event) override; // 重写鼠标按下事件void mouseReleaseEvent(QMouseEvent *event) override; // 重写鼠标释放事件private slots:void on_pushButton_clicked(); // 打开调色板void on_pushButton_2_clicked(); // 设置宽度为1void on_pushButton_3_clicked(); // 设置宽度为5void on_pushButton_4_clicked(); // 设置宽度为10void on_pushButton_5_clicked(); // 切换到画笔模式void on_pushButton_6_clicked(); // 切换到橡皮擦模式private:Ui::Widget *ui;QVector<Line> lines; // 存储所有线段的容器QPoint start; // 线段起点QPoint end; // 线段终点QColor color; // 当前颜色int width; // 当前宽度bool isEraserMode; // 是否处于橡皮擦模式
};#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);color = Qt::black; // 默认颜色width = 1; // 默认宽度isEraserMode = false; // 默认是画笔模式
}Widget::~Widget()
{delete ui;
}void Widget::paintEvent(QPaintEvent *event)
{QPainter painter(this);// 遍历所有线段并绘制for (const auto& line : lines) {QPen pen(line.color, line.width);painter.setPen(pen);painter.drawLine(line.start, line.end);}
}void Widget::mouseMoveEvent(QMouseEvent *event)
{if (event->buttons() & Qt::LeftButton) {end = event->pos();QColor currentColor = isEraserMode ? Qt::white : color; // 橡皮擦模式下使用背景颜色lines.append(Line(start, end, currentColor, width)); // 保存线段信息start = end;update(); // 触发重绘}
}void Widget::mousePressEvent(QMouseEvent *event)
{if (event->button() == Qt::LeftButton) {start = event->pos();}
}void Widget::mouseReleaseEvent(QMouseEvent *event)
{if (event->button() == Qt::LeftButton) {end = event->pos();QColor currentColor = isEraserMode ? Qt::white : color; // 橡皮擦模式下使用背景颜色lines.append(Line(start, end, currentColor, width)); // 保存线段信息update(); // 触发重绘}
}// 打开调色板
void Widget::on_pushButton_clicked()
{color = QColorDialog::getColor(Qt::black, this, "选择颜色");
}// 设置宽度为1
void Widget::on_pushButton_2_clicked()
{width = 1;
}// 设置宽度为5
void Widget::on_pushButton_3_clicked()
{width = 5;
}// 设置宽度为10
void Widget::on_pushButton_4_clicked()
{width = 10;
}// 切换到画笔模式
void Widget::on_pushButton_5_clicked()
{isEraserMode = false; // 设置为画笔模式ui->pushButton_5->setStyleSheet("background-color: lightgray;"); // 高亮画笔模式按钮ui->pushButton_6->setStyleSheet(""); // 取消橡皮擦模式按钮的高亮
}// 切换到橡皮擦模式
void Widget::on_pushButton_6_clicked()
{isEraserMode = true; // 设置为橡皮擦模式ui->pushButton_6->setStyleSheet("background-color: lightgray;"); // 高亮橡皮擦模式按钮ui->pushButton_5->setStyleSheet(""); // 取消画笔模式按钮的高亮
}

运行现象

http://www.lryc.cn/news/546923.html

相关文章:

  • 鸿蒙NEXT开发-端云一体化开发
  • 大模型——股票分析AI工具开发教程
  • nexus 实现https 私有镜像搭建
  • 颈椎X光数据集(cervical spine X-ray dataset)
  • (动态规划 完全背包 零钱兑换)leetcode 322
  • 【AI大模型】DeepSeek + Kimi 高效制作PPT实战详解
  • Pytorch的一小步,昇腾芯片的一大步
  • rabbitmq-amqp事务消息+消费失败重试机制+prefetch限流
  • 【HarmonyOS Next】自定义Tabs
  • Sass 模块化革命:深入解析 @use 语法,打造高效 CSS 架构
  • 【渗透测试】反弹 Shell 技术详解(一)
  • python:pymunk + pygame 模拟六边形中小球弹跳运动
  • Windows 图形显示驱动开发-WDDM 3.2-本机 GPU 围栏对象(二)
  • 23种设计模式之《模板方法模式(Template Method)》在c#中的应用及理解
  • DEV-C++ 为什么不能调试?(正确解决方案)
  • 【C++设计模式】第五篇:原型模式(Prototype)
  • 深入 Vue.js 组件开发:从基础到实践
  • maven导入spring框架
  • 数据守护者:备份文件的重要性与自动化实践策略
  • MyBatis @Param 注解详解:指定的参数找不到?
  • 【项目日记(八)】内存回收与联调
  • 性能测试监控工具jmeter+grafana
  • 016.3月夏令营:数理类
  • CS144 Lab Checkpoint 0: networking warm up
  • 靶场之路-VulnHub-DC-6 nmap提权、kali爆破、shell反连
  • 给没有登录认证的web应用添加登录认证(openresty lua实现)
  • 3月5日作业
  • 【MySQL】增删改查
  • 【三维生成】StarGen:基于视频扩散模型的可扩展的时空自回归场景生成
  • 线反转法实现矩形键盘按键识别