QT-绘画事故

打印 上一主题 下一主题

主题 1730|帖子 1730|积分 5190

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
实现颜色的随时调整,追加橡皮擦功能
widget.h
  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QColor>
  5. #include <QPoint>
  6. #include <QVector>
  7. #include <QMouseEvent>
  8. #include <QPainter>
  9. #include <QColorDialog>
  10. // 自定义 Line 类,存储线段的起点、终点、颜色和宽度
  11. class Line {
  12. public:
  13.     Line(const QPoint& start, const QPoint& end, const QColor& color, int width)
  14.         : start(start), end(end), color(color), width(width) {}
  15.     QPoint start;
  16.     QPoint end;
  17.     QColor color;
  18.     int width;
  19. };
  20. QT_BEGIN_NAMESPACE
  21. namespace Ui { class Widget; }
  22. QT_END_NAMESPACE
  23. class Widget : public QWidget
  24. {
  25.     Q_OBJECT
  26. public:
  27.     Widget(QWidget *parent = nullptr);
  28.     ~Widget();
  29. protected:
  30.     void paintEvent(QPaintEvent *event) override; // 重写绘图事件
  31.     void mouseMoveEvent(QMouseEvent *event) override; // 重写鼠标移动事件
  32.     void mousePressEvent(QMouseEvent *event) override; // 重写鼠标按下事件
  33.     void mouseReleaseEvent(QMouseEvent *event) override; // 重写鼠标释放事件
  34. private slots:
  35.     void on_pushButton_clicked(); // 打开调色板
  36.     void on_pushButton_2_clicked(); // 设置宽度为1
  37.     void on_pushButton_3_clicked(); // 设置宽度为5
  38.     void on_pushButton_4_clicked(); // 设置宽度为10
  39.     void on_pushButton_5_clicked(); // 切换到画笔模式
  40.     void on_pushButton_6_clicked(); // 切换到橡皮擦模式
  41. private:
  42.     Ui::Widget *ui;
  43.     QVector<Line> lines; // 存储所有线段的容器
  44.     QPoint start; // 线段起点
  45.     QPoint end; // 线段终点
  46.     QColor color; // 当前颜色
  47.     int width; // 当前宽度
  48.     bool isEraserMode; // 是否处于橡皮擦模式
  49. };
  50. #endif // WIDGET_H
复制代码
widget.cpp
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. Widget::Widget(QWidget *parent)
  4.     : QWidget(parent)
  5.     , ui(new Ui::Widget)
  6. {
  7.     ui->setupUi(this);
  8.     color = Qt::black; // 默认颜色
  9.     width = 1; // 默认宽度
  10.     isEraserMode = false; // 默认是画笔模式
  11. }
  12. Widget::~Widget()
  13. {
  14.     delete ui;
  15. }
  16. void Widget::paintEvent(QPaintEvent *event)
  17. {
  18.     QPainter painter(this);
  19.     // 遍历所有线段并绘制
  20.     for (const auto& line : lines) {
  21.         QPen pen(line.color, line.width);
  22.         painter.setPen(pen);
  23.         painter.drawLine(line.start, line.end);
  24.     }
  25. }
  26. void Widget::mouseMoveEvent(QMouseEvent *event)
  27. {
  28.     if (event->buttons() & Qt::LeftButton) {
  29.         end = event->pos();
  30.         QColor currentColor = isEraserMode ? Qt::white : color; // 橡皮擦模式下使用背景颜色
  31.         lines.append(Line(start, end, currentColor, width)); // 保存线段信息
  32.         start = end;
  33.         update(); // 触发重绘
  34.     }
  35. }
  36. void Widget::mousePressEvent(QMouseEvent *event)
  37. {
  38.     if (event->button() == Qt::LeftButton) {
  39.         start = event->pos();
  40.     }
  41. }
  42. void Widget::mouseReleaseEvent(QMouseEvent *event)
  43. {
  44.     if (event->button() == Qt::LeftButton) {
  45.         end = event->pos();
  46.         QColor currentColor = isEraserMode ? Qt::white : color; // 橡皮擦模式下使用背景颜色
  47.         lines.append(Line(start, end, currentColor, width)); // 保存线段信息
  48.         update(); // 触发重绘
  49.     }
  50. }
  51. // 打开调色板
  52. void Widget::on_pushButton_clicked()
  53. {
  54.     color = QColorDialog::getColor(Qt::black, this, "选择颜色");
  55. }
  56. // 设置宽度为1
  57. void Widget::on_pushButton_2_clicked()
  58. {
  59.     width = 1;
  60. }
  61. // 设置宽度为5
  62. void Widget::on_pushButton_3_clicked()
  63. {
  64.     width = 5;
  65. }
  66. // 设置宽度为10
  67. void Widget::on_pushButton_4_clicked()
  68. {
  69.     width = 10;
  70. }
  71. // 切换到画笔模式
  72. void Widget::on_pushButton_5_clicked()
  73. {
  74.     isEraserMode = false; // 设置为画笔模式
  75.     ui->pushButton_5->setStyleSheet("background-color: lightgray;"); // 高亮画笔模式按钮
  76.     ui->pushButton_6->setStyleSheet(""); // 取消橡皮擦模式按钮的高亮
  77. }
  78. // 切换到橡皮擦模式
  79. void Widget::on_pushButton_6_clicked()
  80. {
  81.     isEraserMode = true; // 设置为橡皮擦模式
  82.     ui->pushButton_6->setStyleSheet("background-color: lightgray;"); // 高亮橡皮擦模式按钮
  83.     ui->pushButton_5->setStyleSheet(""); // 取消画笔模式按钮的高亮
  84. }
复制代码
运行现象



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

悠扬随风

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表