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()函数中获取鼠标信息即可。
在这里插入图片描述

### pytest_terminal_summary 钩子函数的用途与用法 `pytest_terminal_summary` 是 Pytest 测试框架中的一个钩子函数,它在所有测试运行结束后被调用。这个钩子的主要作用是为用户提供一个接口,以便在测试结束时自定义终端输出的内容和格式[^1]。 #### 1. 钩子函数的作用 `pytest_terminal_summary` 允许开发者在测试完成后执行特定的逻辑,例如统计测试结果、生成报告或输出额外的信息。通过该钩子,可以实现以下功能: - 显示测试运行的总结信息。 - 输出失败测试的具体细节。 - 根据测试结果生成自定义的报告文件(如 HTML 或 XML)[^2]。 #### 2. 使用方法 为了使用 `pytest_terminal_summary`,需要在项目的 `conftest.py` 文件中定义同名函数。该函数通常接收三个参数: - **terminalreporter**:包含测试运行期间收集的所有统计数据。 - **exitstatus**:表示测试运行的退出状态码。 - **config**:当前 Pytest 的配置对象[^3]。 以下是一个典型的实现示例: ```python # conftest.py def pytest_terminal_summary(terminalreporter, exitstatus, config): stats = terminalreporter.stats # 获取测试统计信息 # 输出测试总结信息 print("\nTest Summary:") total_tests = len(stats.get('passed', [])) + len(stats.get('failed', [])) + len(stats.get('skipped', [])) print(f"Total tests run: {total_tests}") print(f"Passed: {len(stats.get('passed', []))}") print(f"Failed: {len(stats.get('failed', []))}") print(f"Skipped: {len(stats.get('skipped', []))}") # 如果有失败的测试,输出失败详情 if len(stats.get('failed', [])) > 0: print("\nFailed tests:") for test in stats.get('failed', []): print(f"{test.nodeid}: {test.longreprtext.splitlines()[0]}") ``` 上述代码会打印测试的总览信息,并列出所有失败的测试用例及其错误原因[^4]。 #### 3. 官方文档参考 Pytest 的官方文档中详细描述了 `pytest_terminal_summary` 的使用方式和参数说明。开发者可以通过阅读文档了解更深入的功能和最佳实践。以下是官方文档链接(需用户自行查阅): [https://docs.pytest.org/en/stable/reference.html#hookspecs](https://docs.pytest.org/en/stable/reference.html#hookspecs) #### 4. 示例 以下是一个完整的项目示例,展示如何使用 `pytest_terminal_summary` 钩子函数: ```python # conftest.py def pytest_terminal_summary(terminalreporter, exitstatus, config): stats = terminalreporter.stats print("\nTest Execution Summary:") print(f"Total tests: {len(stats.get('passed', []) + stats.get('failed', []) + stats.get('skipped', []))}") print(f"Passed: {len(stats.get('passed', []))}") print(f"Failed: {len(stats.get('failed', []))}") print(f"Skipped: {len(stats.get('skipped', []))}") if len(stats.get('failed', [])) > 0: print("\nDetails of failed tests:") for test in stats.get('failed', []): print(f"{test.nodeid}: {test.longreprtext.splitlines()[0]}") # test_example.py def test_success(): assert True def test_failure(): assert False def test_skip(): pytest.skip("Skipping this test") ``` 运行上述代码后,终端将显示详细的测试总结信息,包括成功、失败和跳过的测试数量[^5]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不是很大锅

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

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

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

打赏作者

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

抵扣说明:

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

余额充值