Qt:day4

ops/2025/3/10 2:46:06/

一、作业

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

        2:追加橡皮擦功能;

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

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

【Headers / widget.h】:

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QPaintEvent>
#include <QPainter>
#include <QDebug>
#include <QMouseEvent>
#include <QLine>
#include <QVector>
#include <QColorDialog>
#include <QColor>
#include <QKeyEvent>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private:Ui::Widget *ui;QPainter painter;QPen pen;QPoint start;QPoint end;QVector<QLine> lines;QColor color;QColor previousColor; //橡皮擦模式前的颜色QVector<QColor> colors;int width = 1;QVector<int> widths;bool isEraserMode = false; //橡皮擦模式标志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_pushButton_clicked();void on_pushButton_2_clicked();void on_pushButton_3_clicked();void on_pushButton_4_clicked();void on_pushButton_5_clicked();
};
#endif // WIDGET_H

【Sources / widget.cpp】:

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}void Widget::paintEvent(QPaintEvent *event)
{painter.begin(this);painter.setPen(pen);// 遍历所有线段,并为每条线段设置对应的颜色for(int i = 0; i < lines.size(); ++i){// 使用存储的颜色pen.setColor(colors.at(i));pen.setWidth(widths.at(i));painter.setPen(pen);painter.drawLine(lines.at(i));}painter.end();
}void Widget::mouseMoveEvent(QMouseEvent *event)
{end = event->pos();QLine line(start, end);// 将鼠标绘制的每一根线段存入QVector中,即lines里面lines << line;colors << color;widths << width;// 更新坐标start = end;// 手动触发paintEvent绘图事件update();
}void Widget::mousePressEvent(QMouseEvent *event)
{start = event->pos();
}void Widget::mouseReleaseEvent(QMouseEvent *event)
{end = event->pos();
}void Widget::keyPressEvent(QKeyEvent *event)
{qDebug() << "Warning: 主子,这家伙按下了 Ctrl+Z 呀!!!";if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_Z){if(!lines.isEmpty()){lines.removeLast();colors.removeLast();widths.removeLast();update();}}
}// 打开调色板
void Widget::on_pushButton_clicked()
{color = QColorDialog::getColor(Qt::black, this, "选择颜色");
}// 画笔宽度
void Widget::on_pushButton_2_clicked()
{width = 1;
}void Widget::on_pushButton_3_clicked()
{width = 5;
}void Widget::on_pushButton_4_clicked()
{width = 10;
}
// 橡皮擦
void Widget::on_pushButton_5_clicked()
{// 切换橡皮擦模式isEraserMode = !isEraserMode;if(isEraserMode){// 进入橡皮擦模式,保存当前颜色并将画笔颜色设置为窗口背景色previousColor = color;// 橡皮擦设置为背景色color = palette().color(QPalette::Window);// 按钮文字状态改为“切换到画笔”ui->pushButton_5->setText("切换到画笔");}else{// 退出橡皮擦模式,恢复画笔颜色为之前保存的颜色color = previousColor;// 按钮文字状态改为“橡皮擦”ui->pushButton_5->setText("橡皮擦");}
}

【Sources / main.cpp】:

#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

【测试结果UI】:


http://www.ppmy.cn/ops/164543.html

相关文章

flink重启策略

一、重启策略核心意义 Flink 重启策略&#xff08;Restart Strategy&#xff09;是容错机制的核心组件&#xff0c;用于定义作业在发生故障时如何恢复执行。其核心目标为&#xff1a; 最小化停机时间&#xff1a;快速恢复数据处理&#xff0c;降低业务影响。平衡资源消耗&…

密码学(哈希函数)

4.1 Hash函数与数据完整性 数据完整性&#xff1a; 检测传输消息&#xff08;加密或未加密&#xff09;的修改。 密码学Hash函数&#xff1a; 构建某些数据的简短“指纹”&#xff1b;如果数据被篡改&#xff0c;则该指纹&#xff08;以高概率&#xff09;不再有效。Hash函数…

MyBatis-Plus 与 Spring Boot 的最佳实践

在现代 Java 开发中,MyBatis-Plus 和 Spring Boot 的结合已经成为了一种非常流行的技术栈。MyBatis-Plus 是 MyBatis 的增强工具,提供了许多便捷的功能,而 Spring Boot 则简化了 Spring 应用的开发流程。本文将探讨如何将 MyBatis-Plus 与 Spring Boot 进行整合,并分享一些…

在项目中如何用jmeter进行接口测试

一、接口测试的准备工作 获取接口文档 与开发团队确认接口说明文档&#xff0c;明确接口的请求方式&#xff08;GET/POST&#xff09;、URL、参数&#xff08;Query、Body、Header&#xff09;、响应格式&#xff08;JSON/XML&#xff09;及预期结果410。 若文档缺失&#xff…

http链接转成https的链接的几种方法

以下是一个将HTTP链接转换为HTTPS的JavaScript函数&#xff0c;处理了多种常见输入情况&#xff1a; function convertToHttps(url) {if (typeof url ! string) return url;// 移除首尾空格并处理空字符串const trimmedUrl url.trim();if (!trimmedUrl) return https://;// 替…

案例1_1:Proteus点亮8个蓝色LED灯

文章目录 文章介绍1、原理图2、新建项目文件和.c文件3、代码3.1 源码3.2 生成16进制.hex文件3.3 重建代码3.4 在代码路径中找到.hex文件 4、在原理图中加载代码5、效果图 文章介绍 用Proteus仿真图实现点亮8个led蓝色小灯 1、原理图 2、新建项目文件和.c文件 在STC89C52Study…

OLED屏幕开发全解析:从硬件设计到物联网显示实战 | 零基础入门STM32第五十二步

主题内容教学目的/扩展视频OLED显示屏重点课程电路原理&#xff0c;手册分析&#xff0c;驱动程序。初始化&#xff0c;清屏&#xff0c;ASCII字库&#xff0c;显示分区。调用显示函数。做带有加入图形和汉字显示的RTC时钟界面。讲字库的设计原理。 师从洋桃电子&#xff0c;杜…

C++——类与对象2

类的6个默认成员函数 C中&#xff0c;当类为空的时候&#xff08;没有成员&#xff09;&#xff0c;编译器就什么都不做吗&#xff1f; 其实不是的&#xff0c;这时&#xff0c;编译器就会自动生成6个默认成员函数&#xff1a; 那么&#xff0c;什么是默认成员函数呢&#xf…