该画板功能包括,书写,画直线,矩形,椭圆。并可进行保存,清空,其它功能自行添加
DrawWidget.h
#ifndef DRAWWIDGET_H
#define DRAWWIDGET_H
#include <QWidget>
#include <QPen>
#include <QPainter>
#include <QVector>
#include "Shapes.h"
class DrawWidget :public QWidget
{
Q_OBJECT
public:
DrawWidget(QWidget *parent = nullptr);
Shape::shapes post;
public slots:
void cleanall();
private:
QVector<Shape*> shapelists;
QPoint start_point;
QPoint end_point;
QSize size;
QPainter paint;
Shape *shape;
bool isDraw;
QPen pen;
protected:
void paintEvent(QPaintEvent *e) override;
void mouseMoveEvent(QMouseEvent *e) override;
void mousePressEvent(QMouseEvent *e) override;
void mouseReleaseEvent(QMouseEvent *e) override;
private:
void init_connect();
void drawing();
void initDrawing();
private slots:
void startDraw(QPoint);
void endDraw(QPoint);
void save();
};
#endif // DRAWWIDGET_H
everyShapest.h
#ifndef EVERYSHAPES_H
#define EVERYSHAPES_H
#include "Shapes.h"
#include <QDebug>
class def :public Shape
{
public:
def(shapes shap):Shape(shap)
{
qDebug()<<"default start draw"<<"\n";
post = shap;
}
void draw(QPainter &painter)
{
qDebug()<<"default draw"<<"\n";
painter.drawLine(start, end);
}
};
class line :public Shape
{
public:
line(shapes shap):Shape(shap)
{
post = shap;
}
void draw(QPainter &painter)
{
painter.drawLine(start, end);
}
};
class cricle :public Shape
{
public:
cricle(shapes shap):Shape(shap)
{
post = shap;
}
void draw(QPainter &painter)
{
painter.drawEllipse(start.x(), start.y(),end.x() - start.x(), end.y() - start.y());
}
};
class rectp :public Shape
{
public:
rectp(shapes shap):Shape(shap)
{
post = shap;
}
void draw(QPainter &painter)
{
painter.drawRect(start.x(), start.y(),end.x() - start.x(), end.y() - start.y());
}
};
#endif // EVERYSHAPES_H
mainwindowt.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "drawwidget.h"
#include "Shapes.h"
#include <QString>
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
signals:
void formChange(Shape::shapes);
private:
Ui::MainWindow *ui;
DrawWidget *widget;
QString message;
void initconnect();
bool isout;
private slots:
void on_save_clicked();
void on_clean_clicked();
void on_write_clicked();
void on_line_clicked();
void on_cricle_clicked();
void on_rect_clicked();
void on_out_clicked();
};
#endif // MAINWINDOW_H
Shapes.h
#ifndef SHAPES_H
#define SHAPES_H
#include <QPoint>
#include <QRect>
#include <vector>
#include <QPainter>
class Shape{
public:
enum shapes{
def=0,line,cricle,rect
};
Shape(shapes shap)
{ post = shap;}
virtual void draw(QPainter&) = 0;
void addlist(QPoint p)
{
start = end;
end = p;
pointList.push_back(p);
}
std::vector<QPoint> pointList;
void setStart(QPoint p){ start = p;}
void setEnd(QPoint p) { end = p;}
shapes post;
QPoint start;
QPoint end;
// shapes post;
};
#endif // SHAPES_H
DrawWidget.cpp
#include "drawwidget.h"
#include "everyShapes.h"
#include <QMouseEvent>
#include <QDebug>
DrawWidget::DrawWidget(QWidget *parent)
{
post = Shape::def;
pen.setWidth(2);
pen.setBrush(Qt::SolidPattern);
isDraw = false;
}
void DrawWidget::initDrawing()
{
switch(post)
{
case Shape::def:
{
shape = new def(post);
break;
}
case Shape::line:
{
shape = new line(post);
break;
}
case Shape::cricle:
{
shape = new cricle(post);
break;
}
case Shape::rect:
{
shape = new rectp(post);
break;
}}}
void DrawWidget::startDraw(QPoint p)
{
initDrawing();
start_point = p;
end_point = p;
shape->setEnd(end_point);
shape->setStart(start_point);
isDraw = true;
drawing();
}
void DrawWidget::drawing()
{
if(isDraw)
{
switch(post){
case Shape::def:
{
shape->addlist(end_point);
//this->update();
}
default:
{
shape->setEnd(end_point);
this->update();
}}
}
}
void DrawWidget::endDraw(QPoint p)
{
isDraw = false;
switch(post){
case Shape::def:
{
shape->addlist(end_point);
}
default:
{
shape->setEnd(end_point);
}}
shapelists.append(shape);
}
void DrawWidget::paintEvent(QPaintEvent *e)
{
QPainter paint(this);
paint.setPen(pen);
if(!shapelists.isEmpty())
{
foreach (Shape *shape, shapelists) {
switch (shape->post) {
case Shape::def:
{
QPoint start;
QPoint end = shape->pointList[0];
foreach(QPoint p,shape->pointList)
{
start = end;
end = p;
paint.drawLine(start,end);
}break;
}
default:
{
shape->draw(paint);
break;
}}}}
if(isDraw)
{
if(!shape->pointList.empty())
{
QPoint start;
QPoint end = shape->pointList[0];
foreach(QPoint p,shape->pointList)
{
start = end;
end = p;
paint.drawLine(start,end);
} }
shape->draw(paint);
}}
void DrawWidget::mouseMoveEvent(QMouseEvent *e)
{
qDebug()<<"move"<<"\n";
end_point = e->pos();
drawing();
}
void DrawWidget::mousePressEvent(QMouseEvent *e)
{
qDebug()<<"clicked"<<"\n";
startDraw(e->pos());
}
void DrawWidget::mouseReleaseEvent(QMouseEvent *e)
{
endDraw(e->pos());
}
void DrawWidget::save()
{
}
void DrawWidget::cleanall()
{
shapelists.clear();
this->update();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDateTime>
#include <QScreen>
#include <QPixmap>
#include <QThread>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->widget->hide();
isout = false;//是否弹出
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initconnect()
{
}
void MainWindow::on_save_clicked()
{
on_out_clicked();
QRect rect = ui->centralwidget->geometry();
qDebug()<<rect<<"\n";
QScreen* screen = QApplication::primaryScreen();
QPixmap p = screen->grabWindow(ui->centralwidget->winId(),rect.x(),rect.y(),rect.width(),rect.height());
QString filePathName = "F:/widget/";
filePathName += QDateTime::currentDateTime().toString("MM-dd-hh-mm-ss-zz");
filePathName += ".png";
message = filePathName;
if(!p.save(filePathName,"PNG"))
{
message = "save widget screen failed!!";
}
qDebug()<<message<<"\n";
this->setWindowTitle(this->message);
QThread::sleep(3);
this->setWindowTitle("drawing");
}
void MainWindow::on_clean_clicked()
{
ui->centralwidget->cleanall();
}
void MainWindow::on_line_clicked()
{
ui->centralwidget->post = Shape::line;
}
void MainWindow::on_write_clicked()
{
ui->centralwidget->post = Shape::def;
}
void MainWindow::on_cricle_clicked()
{
ui->centralwidget->post = Shape::cricle;
}
void MainWindow::on_rect_clicked()
{
ui->centralwidget->post = Shape::rect;
}
void MainWindow::on_out_clicked()
{
if(isout) {ui->widget->hide(); isout = false;}
else {ui->widget->show(); isout = true;}
}