Important: Please read the Qt Code of Conduct -

Proxy mouse/touch events

  • There has been some prior discussion, e.g.

    "fake events":

    but I am trying to get this working correctly with Qt5.4 and QQuickView. The simple project below demonstrates. A remote or proxy cursor moves within the main window and if it coincides with an UI element such as a button, a MouseEvent is generated that fakes a users action. The position of the proxy click can be anywhere but the event generated uses the position of the proxy cursor. So the button gets pressed.

    The ClickSimulator class creates an event and sends it to an UI element. This appears to work but the event is never received by the QML id realbutton.

    I am using prebuilt Qt5.4 and I am targeting multiple platforms. This is a genrally useful utility so I hope someone has done this and can spot some problem in my code.

    @#include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QQuickView>
    #include "clicksimulator.h"

    int main(int argc, char *argv[])
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    QObject *topLevel = engine.rootObjects().value(0);
    QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
    QQuickView *view = new QQuickView;
    // Setup the finger click handler
    ClickSimulator sim(window);
    sim.connect(window, SIGNAL(newfingerpos(int, int)),  SLOT(proxyClick(int, int)));
    QObject::connect(view->engine(), SIGNAL(quit()), view, SLOT(close()));
    return app.exec();


    @import QtQuick 2.4
    import QtQuick.Controls 1.3
    import QtQuick.Window 2.2
    import QtQuick.Dialogs 1.2

    ApplicationWindow {
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true

    signal newfingerpos(int x, int y)
    // This is the overlay that catches the finger clicks
    Button {
        objectName: "overlaybutton"
        anchors.fill: parent
        opacity: 0.3
        z: 2
        MouseArea {
            anchors.fill: parent
            propagateComposedEvents: false
            onClicked: {
                mouse.accepted = true
                console.log("overlay tapped")
                newfingerpos(50, 50)
    // This is the 'real' button but it will only be pressed from the newfingerpos signal
    Button {
        id: button
        objectName: "realbutton"
        width: 100; height: 100
        x:25; y:25
        text: qsTr("Press Me")
        z: 1
        MouseArea {
            anchors.fill: parent
            onClicked: {
                console.log("button pressed by proxy event")
                console.log("Mouse x: ", mouseX, "Mouse y: ", mouseY)


    @#pragma once

    #include <QObject>
    #include <QQuickView>

    class ClickSimulator : public QObject

    explicit ClickSimulator(QQuickWindow* viewer, QObject *parent = 0);

    public slots:
    void proxyClick(int, int);

    QQuickWindow* _viewport;


    @#include "clicksimulator.h"

    #include <QMouseEvent>
    #include <QQuickItem>

    ClickSimulator::ClickSimulator(QQuickWindow* viewport, QObject *parent) :

    void ClickSimulator::proxyClick(int x, int y)
    if (_viewport != NULL) {
    QMouseEvent pressEvent(QEvent::MouseButtonPress, QPoint(x, y), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    QQuickItem* controlarea = _viewport->findChild<QQuickItem*>("realbutton");
    const bool isSent = _viewport->sendEvent(controlarea, &pressEvent);
    qDebug() << "'Press' at (" << x << "," << y << ") successful? " << isSent;

        QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPoint(x, y), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
        const bool isRlseSent = _viewport->sendEvent(controlarea, &releaseEvent);
        qDebug() << "'Release' at (" << x << "," << y << ") successful? " << isRlseSent;

    @TEMPLATE = app

    QT += qml quick widgets

    SOURCES += main.cpp

    RESOURCES += qml.qrc

    Additional import path used to resolve QML modules in Qt Creator's code model


    Default rules for deployment.


    HEADERS +=

Log in to reply