新增鼠标定位十字线

master
feiyangqingyun 2019-11-13 15:48:12 +08:00
parent 7dcb02fb68
commit 6a0d95012a
6 changed files with 128 additions and 1 deletions

View File

@ -26,3 +26,4 @@
| 23 | zhtopy | 汉字转拼音 |
| 24 | qwtdemo | qwt的源码版本无需插件直接源码集成到你的项目即可 |
| 25 | buttondefence | 通用按钮地图效果 |
| 25 | mouseline | 鼠标定位十字线 |

11
mouseline/main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}

20
mouseline/mouseline.pro Normal file
View File

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

47
mouseline/widget.cpp Normal file
View File

@ -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());
}

28
mouseline/widget.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
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

20
mouseline/widget.ui Normal file
View File

@ -0,0 +1,20 @@
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>Widget</string>
</property>
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<resources/>
<connections/>
</ui>