Qt之鼠标随手画及画多边形

首先介绍下项目结构,其中

class Figure
class FreeHand
class Polygon
的父类。

class ViewWidget
是画图区,继承自

QWidget

下面介绍具体代码:

main.cpp

#include "Widgets/mainwindow.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
   _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);//内存检漏
   QApplication a(argc, argv);
   MainWindow main;
   main.show();
 
    return a.exec();
}
 

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "Widgets/viewwidget.h"
#include<QMainWindow>
#include<QAction>
#include<QWidget>
 
namespace Ui {
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit MainWindow(QWidget *parent = 0);
    void Init();
    void CreateButtons();
 
 
    ~MainWindow();
 
private:
    QAction *hello_world_action_;
    QToolBar *main_toolbar_;
    QMenu *main_menu_;
    ViewWidget* view_widget_;   //声明ViewWidget指针
 
    Ui::MainWindow *ui;
};
 
#endif // MAINWINDOW_H
 

mainwindow.cpp

#include "Widgets/mainwindow.h"
#include "ui_mainwindow.h"
#include "Widgets/viewwidget.h"
#include<QAction>
#include<QWidget>
#include<QToolBar>
 
using namespace std;
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Init();
 
}
 
 
void  MainWindow::Init()
{
    view_widget_ = new ViewWidget();
    setCentralWidget(view_widget_);
    CreateButtons();
}
 
void MainWindow::CreateButtons()
{
 
    
 
    hello_world_action_ = new QAction(QIcon(":/Mainwindow/picture/Polygon"), tr("&Polygon"), this);  //增加Polygon工具栏
    connect(hello_world_action_, &QAction::triggered, view_widget_, &ViewWidget::set_figure_type_to_Polygon); //响应画多边形
    main_menu_->addAction(hello_world_action_);
    main_toolbar_->addSeparator();
    main_toolbar_->addAction(hello_world_action_);
 
 
 
 
    hello_world_action_ = new QAction(QIcon(":/Mainwindow/picture/FreeHand"), tr("&FreeHand"), this);  //增加FreeHand工具栏
    connect(hello_world_action_, &QAction::triggered, view_widget_, &ViewWidget::set_figure_type_to_FreeHand); //响应画自由曲线
    main_menu_->addAction(hello_world_action_);
    main_toolbar_->addSeparator();
    main_toolbar_->addAction(hello_world_action_);

 
    hello_world_action_ = new QAction(QIcon(":/Mainwindow/picture/Delete"), tr("&Delete"), this);  //增加Delete工具栏
    connect(hello_world_action_, &QAction::triggered, view_widget_, &ViewWidget::delete_last_action);
    main_menu_->addAction(hello_world_action_);
    main_toolbar_->addSeparator();
    main_toolbar_->addAction(hello_world_action_);
 
 
 
    hello_world_action_ = new QAction(tr("&About MiniDraw"), this);
    connect(hello_world_action_, &QAction::triggered, view_widget_, &ViewWidget::set_figure_type_to_Rectangle);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
 

viewwidget.h

#ifndef VIEWWIDGET_H
#define VIEWWIDGET_H
#include <QWidget>
#include <vector>
#include <QPainterPath>
#include <QAction>
 
#include "Resource/figure.h"
#include "Resource/polygon.h"
#include "Resource/freehand.h"
 
 
using namespace std;
enum FigureType;
 
class ViewWidget : public QWidget
{
    Q_OBJECT
public:
    enum FigureType
    {
        kDefault = 0, kLine = 1, kRectangle = 2, kEllipse = 3, kPolygon = 4, kFreeHand = 5,
    };
 
    FigureType figure_type_;
    vector<Figure*> figure_array_;
    vector<Line*>  line_array_;
 
 
 
    explicit ViewWidget(QWidget *parent = 0);
    
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
 
    void paintEvent(QPaintEvent*);
 
 
    void set_figure_type_to_Polygon();
    void set_figure_type_to_FreeHand();
    void delete_last_action();
 
    ~ViewWidget();
 
signals:
 
public slots:
 
private:
  bool  draw_status_;  //绘制状态,true为绘制当前鼠标拖动的图元,false为不绘制
  QPoint start_point_;  //当前图元的起始点
  QPoint end_point_;    //当前图元的终点
  QPoint current_point_;
  vector<QPoint> points;
  QPainterPath  *path;
 
};
 
#endif // VIEWWIDGET_H
 

viewwidget.cpp

#include "Widgets/viewwidget.h"
#include <QPainter>
#include <vector>
#include <QAction>
#include "Widgets/mainwindow.h"
#include <QMessageBox>
#include <QMouseEvent>
 
 
ViewWidget::ViewWidget(QWidget *parent) : QWidget(parent)
{
    draw_status_ = false;  //初始状态为不绘制
    this->path = NULL;
}
 
void ViewWidget::mousePressEvent(QMouseEvent* event)  //鼠标击发响应函数主体
{
    if(Qt::LeftButton == event->button()) //判断鼠标是否左击
    {
        draw_status_ = true;   //设置绘制状态为——绘制
        start_point_ = end_point_ = event->pos();  //将图元初始点设置为当前鼠标击发点
        if(figure_type_ == kPolygon)
        {
            points.push_back(start_point_);
        }
 
        if(figure_type_ == kFreeHand)
        {
 
            path = new QPainterPath;
 
            path->moveTo(event->pos());
 
        }
 
 
    }
}
 
void ViewWidget::mouseMoveEvent(QMouseEvent *event)  //鼠标移动响应函数主体
{
    if(draw_status_)  //判断当前绘制状态
    {
        end_point_ = event->pos();  //若为真,则设置图元终止点为当前鼠标位置
    }
 
    if(figure_type_ == kFreeHand)
    {
        path->lineTo(event->pos());
    }
}
 
void ViewWidget::mouseReleaseEvent(QMouseEvent *event) //鼠标释放响应函数主体
{
 
    Figure* current_figure_ = NULL;
 
    switch(figure_type_)
    {
    case kDefault:
        break;
 
    
 
    case kPolygon:
        points.push_back(end_point_);
        if(Qt::RightButton == event->button())
        {
            current_figure_ = new Polygon(points);
            figure_array_.push_back(current_figure_);
            points.clear();
        }
        break;
 
    case kFreeHand:
        current_figure_ = new FreeHand(*path);
        figure_array_.push_back(current_figure_);
 
        draw_status_ = false;
 
        delete path;
 
        path = NULL;
        break;
 
    default:
        break;
    }
 
 
    current_figure_ = NULL;
 
}
 
void ViewWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this); //定义painter
    for(size_t i = 0; i < figure_array_.size(); i++)
    {
        if(figure_array_[i] != NULL)
            figure_array_[i]->Draw(painter);
    }
 
 
    if(draw_status_)
    {
        switch(figure_type_)
        {
        case kDefault:
            break;
 
        
 
        case kFreeHand:
           std::cout<<"4"<<std::endl;
 
            if(path != NULL)
                painter.drawPath(*path);
            break;
 
        case kPolygon:
            if(points.size()==0)
            {
                break;
            }
            QPoint pre = points[0];
            for(int i=1;i < points.size();i++)
            {
                painter.drawLine(pre,points[i]);
                pre = points[i];
            }
            break;
 
        }
    }
 
    update();
 
}
 
 
 
void ViewWidget::set_figure_type_to_Polygon()
{
    figure_type_ = kPolygon;
}
 
void ViewWidget::set_figure_type_to_FreeHand()
{
    figure_type_ = kFreeHand;
 
}
 
 
void ViewWidget::delete_last_action()
{
    if(!figure_array_.empty())
    {
        delete figure_array_.back();
        figure_array_.pop_back();
    }
 
}
 
 
 
ViewWidget::~ViewWidget()
{
    for(size_t i = 0; i < figure_array_.size(); i++)
    {
        if(figure_array_[i])
        {
            delete figure_array_[i];
        }
    }
    figure_array_.clear();
}
 
 

figure.h

#ifndef FIGURE_H
#define FIGURE_H
#include <QPainter>
 
class Figure
{
public:
     Figure();
 
    virtual void Draw(QPainter &);
    virtual ~Figure();
};
 
#endif // FIGURE_H
 

figure.cpp

#include "Resource/figure.h"
#include<QPainter>
 
Figure::Figure()
{
 
}
 
Figure::~Figure()
{
 
}
void Figure::Draw(QPainter &)
{
 
}
 

freehand.h

#ifndef FREEHAND_H
#define FREEHAND_H
#include"Resource/figure.h"
#include<QPainter>
#include<QPainterPath>
 
class FreeHand : public Figure
{
public:
    FreeHand();
    FreeHand( QPainterPath paths);
 
    void Draw(QPainter &paint);
//    void addPoint(QPoint point);
 
    ~FreeHand();
 
private:
 
  QPainterPath path;
//  QVector<QPoint> points;
};
 
#endif // FREEHAND_H
 

freehand.cpp

#include "Resource/freehand.h"
#include <QPainter>
#include <QPainterPath>
#include <iostream>
FreeHand::FreeHand(QPainterPath paths)
{
    path = paths;
}
 
void FreeHand::Draw(QPainter& paint)
{
    paint.drawPath(path);
}
 
FreeHand::~FreeHand()
{
 
}
 

polygon.h

#ifndef POLYGON_H
#define POLYGON_H
#include "Resource/figure.h"
#include<vector>
 
using namespace std;
 
class Polygon : public Figure
{
public:
    Polygon();
    Polygon(const vector<QPoint> points);
 
   void Draw(QPainter& paint);
    ~Polygon();
 
private:
    vector<QPoint> point;
 
};
 
#endif // POLYGON_H
 

polygon.cpp

#include "Resource/polygon.h"
#include <QPainter>
#include <vector>
 
using namespace std;
 
Polygon::Polygon()
{
 
}
 
Polygon::Polygon(const vector<QPoint> points)
{
    point = points;
}
 
void Polygon::Draw(QPainter &paint)
{
   paint.drawPolygon(&point.at(0),point.size());
}
 
Polygon::~Polygon()
{
 
}
 
 

好了,以上是这个程序的代码,我是第一次写博客,也不知道怎么说,就直接贴代码了。


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值