1 comm.h
#ifndef COMMON_H
#define COMMON_H
struct PointEx {
double x;
double y;
PointEx(double a, double b)
{
x = a;
y = b;
}
PointEx() {}
};
struct LineSegment {
PointEx startPoint;
PointEx endPoint;
LineSegment(PointEx a, PointEx b)
{
startPoint = a;
endPoint = b;
}
LineSegment() {}
};
struct LineInfo {
bool bDraw; //是否绘制
bool bSelLine; //是否选中线
bool bSelStartPt; //是否选中线段起点
bool bSelEndPt; //是否选线段终点
LineSegment* seg;
LineInfo()
{
bDraw = false;
bSelLine = false;
bSelStartPt = false;
bSelEndPt = false;
seg = new LineSegment;
}
~LineInfo() { delete seg; }
};
#endif // COMMON_H
2 qdrawingpaperview.h
#ifndef COMMON_H
#define COMMON_H
struct PointEx {
double x;
double y;
PointEx(double a, double b)
{
x = a;
y = b;
}
PointEx() {}
};
struct LineSegment {
PointEx startPoint;
PointEx endPoint;
LineSegment(PointEx a, PointEx b)
{
startPoint = a;
endPoint = b;
}
LineSegment() {}
};
struct LineInfo {
bool bDraw; //是否绘制
bool bSelLine; //是否选中线
bool bSelStartPt; //是否选中线段起点
bool bSelEndPt; //是否选线段终点
LineSegment* seg;
LineInfo()
{
bDraw = false;
bSelLine = false;
bSelStartPt = false;
bSelEndPt = false;
seg = new LineSegment;
}
~LineInfo() { delete seg; }
};
#endif // COMMON_H
3 widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMainWindow>
#include "qdrawingpaperview.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QMainWindow
{
Q_OBJECT
public:
QDrawingPaperView *m_drawingPaperView = nullptr;
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
4 main.cpp
#include "widget.h"
#include <QApplication>
// 这个是绘图
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.setWindowFlags(Qt::FramelessWindowHint); // 设置无边框窗口
w.show();
return a.exec();
}
5 qdrawingpaperview.cpp
#include "qdrawingpaperview.h"
#include <QMouseEvent>
#include <QPainter>
QDrawingPaperView::QDrawingPaperView(QWidget *parent)
: QWidget(parent)
, m_bLBtnDown(false)
, m_currectSelectedLine(nullptr)
{
}
LineInfo *QDrawingPaperView::getSeleledLine() { return nullptr; }
void QDrawingPaperView::mousePressEvent(QMouseEvent *event)
{
switch (event->button()) {
case Qt::LeftButton:
m_bLBtnDown = true; // 记录左键鼠标按下
m_currectSelectedLine = nullptr;
m_currectSelectedLine = getSeleledLine();
if (nullptr == m_currectSelectedLine) { // 未选中
m_startPoint = event->pos();
m_endPoint = m_startPoint;
// 记录绘画起始点
m_tempLine = new LineInfo;
m_tempLine->seg->startPoint.x = event->x();
m_tempLine->seg->startPoint.y = event->y();
}
break;
default:
break;
}
}
void QDrawingPaperView::mouseReleaseEvent(QMouseEvent *event)
{
switch (event->button()) {
case Qt::LeftButton:
m_bLBtnDown = false; // 记录左键鼠标按下
if (nullptr == m_currectSelectedLine) {
// 记录绘画结束点
m_tempLine->seg->endPoint.x = event->pos().x();
m_tempLine->seg->endPoint.y = event->pos().y();
m_tempLine->bDraw = true;
// 将线段信息保存在列表中
m_listLineInfos.push_back(m_tempLine);
}
break;
default:
break;
}
}
void QDrawingPaperView::mouseMoveEvent(QMouseEvent *event)
{
QPointF movePt = event->pos();
if (m_bLBtnDown) {
m_endPoint = movePt;
}
update();
}
void QDrawingPaperView::paintEvent(QPaintEvent *event)
{
QPainter p(this);
// 设置画笔的样式
p.setPen(QPen(Qt::red, 2));
// 绘制临时图像
if (m_bLBtnDown == true) {
p.drawLine(m_startPoint, m_endPoint);
}
// 绘制最终的数据列表
foreach (auto item, m_listLineInfos) {
if (true == item->bDraw) {
p.drawLine(item->seg->startPoint.x, item->seg->startPoint.y,
item->seg->endPoint.x, item->seg->endPoint.y);
}
}
}
6 widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
m_drawingPaperView = new QDrawingPaperView(this);
m_drawingPaperView->setMouseTracking(true);
this->setCentralWidget(m_drawingPaperView);
this->resize(700, 500);
this->setWindowTitle("绘图板");
}
Widget::~Widget()
{
delete ui;
}