Qt unit testing QGraphicsView - how to keep the mouse “pressed”?

I have a pretty complicated QGraphicsView / Scene setting where I have elements with complex interactions.

As such, I want a unit test to avoid creating errors in existing functions. For one test, I want:

  • Click on an item in the scene.
  • Move your mouse right
  • Release the mouse button

This will allow me to verify that the item has been selected, has been moved by the desired amount and not selected.

However, I found that after dispatching mouseMove events, the state of the mouse 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)

Where gv is QGraphicsView.

The problem seems to be that mouseMove events are visible as hoverMoveEvents using QGraphicsItem - this should be treated as mouseMoveEvent!

:

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

, , " "?

:

unit test /

Edit:

TL;DR; ? , QGraphicsItems mouseHover mouseMove.

+5
1

- , :

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 - , .

, QTest , , , , . !

+6

All Articles