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

Qt:day4

一、作业

        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_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    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】:

相关文章:

  • DeepSeek本机部署(基于Ollama和Docker管理)
  • 第六十:跨组件通信-依赖注入(父传递给其他组件)
  • C# | 委托 | 事件 | 异步
  • Varjo XR-4 混合现实驾驶仿真解决方案
  • 迷你世界脚本UI五子棋小游戏
  • JDBC 完全指南:掌握 Java 数据库交互的核心技术
  • SpringBoot为什么要禁止循环依赖?
  • 从零开始的 Kafka 学习(一)| 概念,Java API
  • ⭐算法OJ⭐跳跃游戏【动态规划 + 单调队列】(C++实现)Jump Game 系列 VI
  • 场景题:10亿QQ用户,如何统计在线人数?
  • 2025最新在GitHub上搭建个人图床,保姆级图文教程,实现图片高效管理
  • 通过RK3588的cc-linaro-7.5.0交叉编译器搭建QT交叉编译环境QtCreator(无需编译QT源码)
  • 将数据库结构化数据整合到RAG问答中的方式
  • android .rc文件
  • 【图像识别UI自动测试技术第二章】模版匹配算法学习分享
  • office或者word排版中,复制/黏贴进来文字不会自动换行,如何处理?
  • 系统架构设计师—计算机基础篇—进度管理
  • 在线研讨会 | 加速游戏和AI应用,全面认识Imagination DXTP GPU
  • 防火墙虚拟系统实验
  • leetcode 1328. 破坏回文串 中等
  • 龙湾网站建设/杭州seo关键词优化公司
  • wordpress图标方块/百度seo指数查询
  • 网站收录怎么删/百度联盟点击广告赚钱
  • 做个有用网站/大数据营销策略有哪些
  • 武汉专业做网站开发的公司/教育培训学校
  • 非经营备案网站能贴放广告么/营销推广内容