QT实现绘图功能

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 <QKeyEvent>

struct Lineinfo{
    QLine line;
    QColor color;
    int width;
};


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<Lineinfo> lines;
    QVector<QPoint> lines_last;
    QColor color;
    QColor recolor;
    int width=1;
    int re=0;

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();
    void on_pushButton_6_clicked();
};
#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);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    painter.begin(this);

    for(auto perline:lines){
        pen.setColor(perline.color);
        pen.setWidth(perline.width);
        painter.setPen(pen);
        painter.drawLine(perline.line);
    }

    painter.end();
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    end = event->pos();
    QLine line(start,end);
    Lineinfo lineinfo;
    lineinfo.color=color;
    lineinfo.line=line;
    lineinfo.width=width;
    lines.append(lineinfo);

    // 将鼠标绘制的每一跟线段存入QVector里面,也就是lines里面

    start = end;
    update();
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    start = event->pos();
        lines_last<<start;
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    end = event->pos();

}

void Widget::keyPressEvent(QKeyEvent *event)
{
      if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_Z)
      {
         if(!lines_last.isEmpty()){
            while(lines.last().line.p1() != lines_last.last())
              {
                  lines.removeLast();
              }
          lines_last.removeLast();
          lines.removeLast();
         }
     }

     update();
}

// 打开调色板
void Widget::on_pushButton_clicked()
{
    color = QColorDialog::getColor(Qt::black,this,"选择颜色");
}

void Widget::on_pushButton_2_clicked()
{
    width = 1;
    if(re==1){color=recolor;}
}

void Widget::on_pushButton_3_clicked()
{
    width = 5;
     if(re==1){color=recolor;}
}

void Widget::on_pushButton_4_clicked()
{
    width = 10;
     if(re==1){color=recolor;}
}

void Widget::on_pushButton_5_clicked()
{
    recolor=color;
    color=palette().color(QPalette::Window);
    re=1;
}
相关推荐
悟能不能悟1 小时前
能刷java题的网站
java·开发语言
IT古董1 小时前
【第四章:大模型(LLM)】05.LLM实战: 实现GPT2-(6)贪婪编码,temperature及tok原理及实现
android·开发语言·kotlin
程序员陆通2 小时前
Java高并发场景下的缓存穿透问题定位与解决方案
java·开发语言·缓存
阿群今天学习了吗2 小时前
“鱼书”深度学习进阶笔记(3)第四章
人工智能·笔记·python·深度学习·算法
IT猿手2 小时前
2025年最新原创多目标算法:多目标酶作用优化算法(MOEAO)求解MaF1-MaF15及工程应用---盘式制动器设计,提供完整MATLAB代码
算法·数学建模·matlab·多目标优化算法·多目标算法
澡点睡觉2 小时前
golang的继承
开发语言·后端·golang
洛阳泰山4 小时前
基于 Easy Rules 的电商订单智能决策系统:构建可扩展的业务规则引擎实践
java·开发语言·规则引擎·easy rules
kushu75 小时前
Java 包
java·开发语言
xiaobobo33305 小时前
C语言中关于普通变量和指针变量、结构体包含子结构体或包含结构体指针的一些思考
c语言·开发语言·结构体指针
java1234_小锋6 小时前
周学会Matplotlib3 Python 数据可视化-绘制折线图(Lines)
开发语言·python·信息可视化·matplotlib·折线图·matplotlib3