We do this in the MouseArea autotests:

    QGraphicsScene *scene = canvas->scene();
    QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress);
    pressEvent.setScenePos(QPointF(100, 100));
    pressEvent.setButton(Qt::LeftButton);
    pressEvent.setButtons(Qt::LeftButton);
    QApplication::sendEvent(scene, &pressEvent);

Keep in mind that the position set above is in scene coordinates.

BR,
Martin.


From: qt-qml-boun...@trolltech.com [mailto:qt-qml-boun...@trolltech.com] On 
Behalf Of Wangbing Kevin (Nokia-MS/Beijing)
Sent: Monday, 13 December 2010 7:46 PM
To: qt-qml@trolltech.com
Subject: [Qt-qml] How to simulate mouse event

Hi,

I'm a freshman on qml plug-in development.
I want to simulate key and mouse events for testing my qml script automatically.
Now, I can simulate the key event successfully by 
"item->scene()->sendEvent(item, &keyEvent);"

But it cannot work when I call "item->scene()->sendEvent(item, &mouseEvent);" 
to simulate mouse event.
I've tried another way "QApplication::sendEvent(view, &mouseEvent);", but it 
still cannot work.
The return value is always false.

My qml script is follow, it can output log when I click actual mouse key.
Rectangle {
   id: container
......

MouseArea {

       id: mouseArea

       anchors.fill: parent

       onClicked: {

           mouse.accepted  = true;

           console.log("Mouse Clicked! key: " + mouse.button + " | keys: " + 
mouse.buttons);

       }
}

Do you have any suggestion?

Best Regards,

Kevin Wang
_______________________________________________
Qt-qml mailing list
Qt-qml@trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-qml

Reply via email to