How to inject synthetised events in Qt5 SceneGraph

  • Hello all,

    I'm in Qt5.3.2.
    I need to synthetise and inject interaction events (Mouse events, keyboard events, touch events), into a QML Scene (Qt5 scenegraph).
    I have a pluggin that correctly receive interaction commands from network, but I can't manage to re-inject those events into the scene graph.
    Here is my code for synthetising and injecting events into the scenegraph :

    void InteractionInjector::injectMouseEvent(/*BlaBlaBla...*/)
        QQuickWindow *lWindow = (QQuickWindow *)QGuiApplication::allWindows().first();
        QQuickItem* lRootItem = lWindow->contentItem();
        /*  Recursively send event to all objects in the scenegraph) */
        recurseInjectMouseEvent(lWindow, lRootItem, m_mouseScenePos,
                                (m_mouseButtonState == Pressed)?  QEvent::GraphicsSceneMousePress : 
    void InteractionInjector::recurseInjectMouseEvent(QQuickWindow *pWindow, QQuickItem *pRootItem, QPoint pScenePos, QEvent::Type pEvent)
        QGraphicsSceneMouseEvent* lEvent;
        bool lResult = false;
        lEvent = new QGraphicsSceneMouseEvent(pEvent);
        lEvent->setButton  (Qt::LeftButton);
        lEvent->setButtons (Qt::LeftButton);
        lResult = pWindow->sendEvent(pRootItem, lEvent);        /*  I always get lResult = false  (why ?)   */
        QApplication::postEvent((QObject*)pRootItem, lEvent);   /*  I also tryed this ....              */
        /*  Recursively iterate through scenegraph hyerarchy  */
        foreach(QQuickItem* lChild, pRootItem->childItems())
            stc_recurseSend(pWindow, lChild, pScenePos, pEvent);

    In the QML file:

        MouseArea {
            anchors.fill: parent
            onPressed:  console.log("MouseArea::onPressed");
            onReleased: console.log("MouseArea::onReleased");
            onClicked:  console.log("MouseArea::onClicked");

    I can't manage to get my QML MouseArea's onPressed, onReleased, or onClicked called from my pluggin.
    As I mentioned as a comment in the code, I alwas get lResult=false, for all widgets in the scenegraph, but I don't know why ?
    Am I doing something wrong ?
    Do I really have to parse all the objects in the scenegraph to inject an event, or is there a kind of
    "lWindow->getSceneGraph()->sendEvent(...)" somewhere ?

    Note: The same kind of code was working in Qt4, but with Qt5 ScenGraph I can't manage to get this functionnality to work again.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.