QT 跟踪鼠标轨迹

QT 跟踪鼠标轨迹

一.普通窗口的鼠标轨迹

1.获取鼠标的位置,可以通过调用窗口的鼠标移动事件函数 mouseMoveEvent(QMouseEvent *);

  • 再通过鼠标事件的 pos()函数获取鼠标位置。

    但是,仅指定mouseMoveEvent(QMouseEvent *)函数,还不够,此时,需要在窗口内按下鼠标左键或者右键,然后才能出发 mouseMoveEvent(QMouseEvent *) 函数。

2.而很多情况下,我们希望的是,不按下鼠标,仅仅移动鼠标就可以获取鼠标的轨迹。该怎么做?
查询,Qt 助手资料:
[virtual protected] void QWidget::mouseMoveEvent(QMouseEvent *event)
This event handler, for event event, can be reimplemented in a subclass to receive mouse move events for the widget.
If mouse tracking is switched off, mouse move events only occur if a mouse button is pressed while the mouse is being moved. If mouse tracking is switched on, mouse move events occur even if no mouse button is pressed.
QMouseEvent::pos() reports the position of the mouse cursor, relative to this widget. For press and release events, the position is usually the same as the position of the last mouse move event, but it might be different if the user’s hand shakes. This is a feature of the underlying window system, not Qt.
If you want to show a tooltip immediately, while the mouse is moving (e.g., to get the mouse coordinates with QMouseEvent::pos() and show them as a tooltip), you must first enable mouse tracking as described above. Then, to ensure that the tooltip is updated immediately, you must call QToolTip::showText() instead of setToolTip() in your implementation of mouseMoveEvent().
See also setMouseTracking(), mousePressEvent(), mouseReleaseEvent(), mouseDoubleClickEvent(), event(), QMouseEvent, and Scribble Example.

  • 意思是说,还需要对对应的窗口调用 setMouseTracking(true) 函数。
  • ps:对于QWidget的嵌套,子窗口要不按下鼠标就能捕获鼠标轨迹,只设置子窗口的 setMouseTracking(true) 是不够的!还需要对每一层父窗口设置 setMouseTracking(true)。

3.举个栗子说明吧,新建一个普通mainwindow 工程,然后在 centralWidget 中拖入一个widget。在mouseMoveEvent()中获取鼠标的坐标位置,并通过label显示出来。代码如下:
mianwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QMouseEvent>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    QLabel *label;

protected:
    void mouseMoveEvent(QMouseEvent *p);
};

#endif // MAINWINDOW_H

mianwindow.h.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    label = new QLabel(this);

    this->setMouseTracking(true);
    ui->centralWidget->setMouseTracking(true);
    ui->widget->setMouseTracking(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::mouseMoveEvent(QMouseEvent *p)
{
    qDebug()<<p<<p->pos();
    QString text;
    text.sprintf( "(%d,%d)", p->x(), p->y() );
    label->setText(text);
    label->move(p->pos());
}

在这里插入图片描述

二.QChartView 实现鼠标跟踪

问题

前几天,遇到一个问题,使用QChart模块画图表,然后,打算实现功能:鼠标移动时,显示当前线条上的坐标点。实现过程中,发现如果对QChartView和其父窗口设置 setMouseTracking(true),是不能实现鼠标移动时,就可以获取鼠标位置,必须要按下鼠标才可以。

解决方法

原来,QChartView 是有自己的mouseEvent()的,所以,要实现不按下鼠标就可捕获鼠标轨迹,要自己重新定义一个类,继承QChartView ,然后在其mouseEvent()函数中获取鼠标信息即可。
在这里插入图片描述

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不是很大锅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值