From 6a0d95012ae854e2a8c2a8503ce43793fff1e115 Mon Sep 17 00:00:00 2001 From: feiyangqingyun Date: Wed, 13 Nov 2019 15:48:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=BC=A0=E6=A0=87=E5=AE=9A?= =?UTF-8?q?=E4=BD=8D=E5=8D=81=E5=AD=97=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- mouseline/main.cpp | 11 ++++++++++ mouseline/mouseline.pro | 20 ++++++++++++++++++ mouseline/widget.cpp | 47 +++++++++++++++++++++++++++++++++++++++++ mouseline/widget.h | 28 ++++++++++++++++++++++++ mouseline/widget.ui | 20 ++++++++++++++++++ 6 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 mouseline/main.cpp create mode 100644 mouseline/mouseline.pro create mode 100644 mouseline/widget.cpp create mode 100644 mouseline/widget.h create mode 100644 mouseline/widget.ui diff --git a/README.md b/README.md index 584caaa..6b1ecaa 100644 --- a/README.md +++ b/README.md @@ -25,4 +25,5 @@ | 22 | lineeditnext | 文本框回车焦点下移 | | 23 | zhtopy | 汉字转拼音 | | 24 | qwtdemo | qwt的源码版本,无需插件,直接源码集成到你的项目即可 | -| 25 | buttondefence | 通用按钮地图效果 | \ No newline at end of file +| 25 | buttondefence | 通用按钮地图效果 | +| 25 | mouseline | 鼠标定位十字线 | \ No newline at end of file diff --git a/mouseline/main.cpp b/mouseline/main.cpp new file mode 100644 index 0000000..90b6d53 --- /dev/null +++ b/mouseline/main.cpp @@ -0,0 +1,11 @@ +#include "widget.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + + return a.exec(); +} diff --git a/mouseline/mouseline.pro b/mouseline/mouseline.pro new file mode 100644 index 0000000..0244dcc --- /dev/null +++ b/mouseline/mouseline.pro @@ -0,0 +1,20 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2018-08-13T14:27:21 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = mouseline +TEMPLATE = app + + +SOURCES += main.cpp\ + widget.cpp + +HEADERS += widget.h + +FORMS += widget.ui diff --git a/mouseline/widget.cpp b/mouseline/widget.cpp new file mode 100644 index 0000000..76d6211 --- /dev/null +++ b/mouseline/widget.cpp @@ -0,0 +1,47 @@ +#include "widget.h" +#include "ui_widget.h" +#include "qpainter.h" +#include "qevent.h" +#include "qdebug.h" + +Widget::Widget(QWidget *parent) : + QWidget(parent), + ui(new Ui::Widget) +{ + ui->setupUi(this); + this->setMouseTracking(true); +} + +Widget::~Widget() +{ + delete ui; +} + +void Widget::mouseMoveEvent(QMouseEvent *event) +{ + lastPos = event->pos(); + update(); +} + +void Widget::mouseReleaseEvent(QMouseEvent *event) +{ + //这里是鼠标按下的坐标,自己存到数据库 + lastPos = event->pos(); + update(); + qDebug() << lastPos; +} + +void Widget::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + + QPen pen; + pen.setWidth(5); + pen.setColor(Qt::red); + painter.setPen(pen); + + //绘制横向线 + painter.drawLine(0, lastPos.y(), width(), lastPos.y()); + //绘制纵向线 + painter.drawLine(lastPos.x(), 0, lastPos.x(), height()); +} diff --git a/mouseline/widget.h b/mouseline/widget.h new file mode 100644 index 0000000..ce4cb0c --- /dev/null +++ b/mouseline/widget.h @@ -0,0 +1,28 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include + +namespace Ui { +class Widget; +} + +class Widget : public QWidget +{ + Q_OBJECT + +public: + explicit Widget(QWidget *parent = 0); + ~Widget(); + +protected: + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + void paintEvent(QPaintEvent *); + +private: + Ui::Widget *ui; + QPoint lastPos; +}; + +#endif // WIDGET_H diff --git a/mouseline/widget.ui b/mouseline/widget.ui new file mode 100644 index 0000000..8541684 --- /dev/null +++ b/mouseline/widget.ui @@ -0,0 +1,20 @@ + + Widget + + + + 0 + 0 + 400 + 300 + + + + Widget + + + + + + +