单元测试——如何让鼠标保持

单元测试——如何让鼠标保持

本文介绍了Qt QGraphicsView 单元测试——如何让鼠标保持“按下"状态状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当复杂的 QGraphicsView/Scene 设置,其中我有具有复杂交互的项目.

I have a fairly complex QGraphicsView/Scene setup where by I have items with complex interactions.

因此,我想对此进行单元测试,以避免在现有功能中产生错误.对于一项测试,我希望:

As such I want to unit test this to avoid creating bugs in already existing functionality. For one test I wish to:

  1. 在场景中的一个项目上按下鼠标
  2. 向右移动鼠标
  3. 松开鼠标

这将允许我检查该项目是否被选中、移动了正确的数量以及是否被取消选中.

This will allow me to check that the item was selected, was moved by the correct amount, and was deselected.

但是我发现在发送 mouseMove 事件后鼠标状态变为已释放",这是我的代码:

However I find that after sending mouseMove events the mouse state becomes "released", here is my code:

QTest.mousePress(gv.viewport(), Qt.LeftButton, Qt.NoModifier, QPoint(80,80), 100)
QTest.mouseMove(gv.viewport(), QPoint(80,80), 200)
QTest.mouseMove(gv.viewport(), QPoint(90,80), 300)
QTest.mouseMove(gv.viewport(), QPoint(100,80), 400)
QTest.mouseRelease(gv.viewport(), Qt.LeftButton, Qt.NoModifier, QPoint(80,80), 900)

其中 gv 是 QGraphicsView.

Where gv is a QGraphicsView.

问题似乎是 mouseMove 事件被 QGraphicsItem 视为 hoverMoveEvents - 它应该被视为 mouseMoveEvent!

The problem seems to be that the mouseMove events are seen as hoverMoveEvents by the QGraphicsItem - it should be seen as a mouseMoveEvent!

根据文档:

http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#setAcceptHoverEvents

那么这些模拟事件似乎没有设置鼠标抓取项目"?

So it would seem that these simulated events do not set the "mouse grabber item"?

相关:

如何对 qt 图形视图小部件/项目进行单元测试

TLDR;为什么我的假鼠标事件没有设置当前的鼠标抓取器项目?这会导致 QGraphicsItems 获取 mouseHover 事件而不是 mouseMove 事件.

TLDR; Why are my fake mouse events not setting the current mouse grabber item? This causes QGraphicsItems to get mouseHover events instead of mouseMove events.

推荐答案

终于得到了一些实际可行的东西:

Finally managed to get something that actually works:

w = gv.viewport()

# this actually gets the click to the view
#QTest.mouseMove(w, QPoint(80,80))
event = QMouseEvent(QEvent.MouseMove, QPoint(80,80), w.mapToGlobal(QPoint(80,80)), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier);
#event.setSpontaneous(True)
QApplication.postEvent(w, event);
QTest.qWait(250)

#QTest.mouseMove(w, QPoint(80,80))
event = QMouseEvent(QEvent.MouseButtonPress, QPoint(80,80), w.mapToGlobal(QPoint(80,80)), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier);
QApplication.postEvent(w, event);
QTest.qWait(250)

count = 0
while count < 20:

    #QTest.mouseMove(w, QPoint(80+count,80+count))
    event = QMouseEvent(QEvent.MouseMove, QPoint(80+count,80+count), w.mapToGlobal(QPoint(80+count,80+count)), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier);
    QApplication.postEvent(w, event);
    t = w.mapToGlobal(QPoint(80+count,80+count))
    #QCursor.setPos(t)
    QTest.qWait(20)
    count = count + 1

event = QMouseEvent(QEvent.MouseButtonRelease, QPoint(100,100), w.mapToGlobal(QPoint(100,100)), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier);
QApplication.postEvent(w, event);

为什么这些捏造的事件有效而 QTest 的无效我不知道..

Why these fabricated events work and the QTest ones don't I have no idea..

似乎 QTest 会物理移动鼠标,而此代码的行为就好像鼠标已经移动但没有移动.混淆我知道!

It appears that QTest will physically move the mouse, where as this code acts as if the mouse had moved but hasn't. Confusing I know!

这篇关于Qt QGraphicsView 单元测试——如何让鼠标保持“按下"状态状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 16:11