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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值