Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. qml How to simulate mouse events
Forum Updated to NodeBB v4.3 + New Features

qml How to simulate mouse events

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 4.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jimfar
    wrote on last edited by
    #1

    i want to send a mouse event to the MouseArea of a qml object ,How do you implement this feature? anyone any idea

    Pablo J. RoginaP 1 Reply Last reply
    0
    • J jimfar

      i want to send a mouse event to the MouseArea of a qml object ,How do you implement this feature? anyone any idea

      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

      @jimfar given that MouseEvent and TouchEvent are equivalent from QML MouseArea point of view per this, I'd use the TouchEventSequence QML Type from the Qt Quick Test module.

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      C 1 Reply Last reply
      0
      • J Offline
        J Offline
        jimfar
        wrote on last edited by
        #3

        i am desktop app , is this way to work out this problem

        1 Reply Last reply
        0
        • Pablo J. RoginaP Pablo J. Rogina

          @jimfar given that MouseEvent and TouchEvent are equivalent from QML MouseArea point of view per this, I'd use the TouchEventSequence QML Type from the Qt Quick Test module.

          C Offline
          C Offline
          codezeug
          wrote on last edited by
          #4

          TouchEventSequence must apparently be created by TestCase.touchEvent().
          Is there a way to use TouchEventSequence in a normal QML application that is not in unit testing mode?
          Thank you.

          1 Reply Last reply
          0
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            @jimfar said in qml How to simulate mouse events:

            How do you implement this feature?

            Like this:
            main.cpp

            #include <QGuiApplication>
            #include <QQmlApplicationEngine>
            #include <QQmlContext>
            
            #include <QMouseEvent>
            
            class GenMouseEvent : public QObject
            {
                Q_OBJECT
            public:
                GenMouseEvent(QObject* parent=nullptr)
                    : QObject(parent)
                {
                }
            
            public slots:
                void sendMouseEvent(QObject* object, QPointF pos, int button){
                    Qt::MouseButton type = Qt::MouseButton(button);
                    auto down = new QMouseEvent(QMouseEvent::Type::MouseButtonPress, pos, type, type, Qt::KeyboardModifier::NoModifier);
                    auto up = new QMouseEvent(QMouseEvent::Type::MouseButtonRelease, pos, type, type, Qt::KeyboardModifier::NoModifier);
                    QCoreApplication::postEvent(object, down);
                    QCoreApplication::postEvent(object, up);
                }
            };
            
            int main(int argc, char *argv[])
            {
                QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
            
                QGuiApplication app(argc, argv);
            
                QQmlApplicationEngine engine;
            
                GenMouseEvent gme;
            
                auto context = engine.rootContext();
                context->setContextObject(&gme);
            
                const QUrl url(QStringLiteral("qrc:/main.qml"));
                QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                                 &app, [url](QObject *obj, const QUrl &objUrl) {
                    if (!obj && url == objUrl)
                        QCoreApplication::exit(-1);
                }, Qt::QueuedConnection);
                engine.load(url);
            
                return app.exec();
            }
            
            #include "main.moc"
            

            main.qml

            import QtQuick 2.12
            import QtQuick.Controls 2.12
            import QtQuick.Window 2.12
            
            Window {
                visible: true
                width: 640
                height: 480
                title: qsTr("Simulate MouseEvent")
            
                Button {
                    id: button
            
                    text: "Test Button"
                    onClicked: {
            
                        console.log("onClicked")
                    }
                }
            
                MouseArea {
                    id: mousearea
            
                    acceptedButtons: Qt.LeftButton | Qt.RightButton
            
                    onClicked: {
                        if(mouse.button === Qt.LeftButton){
                            console.log("ma clicked left")
                        }
                        if(mouse.button === Qt.RightButton){
                            console.log("ma clicked right")
                        }
                    }
                }
            
                Component.onCompleted: {
                    sendMouseEvent(button, Qt.point(0,0), Qt.LeftButton)
                    sendMouseEvent(button, Qt.point(0,0), Qt.RightButton)
            
                    sendMouseEvent(mousearea, Qt.point(0,0), Qt.LeftButton)
                    sendMouseEvent(mousearea, Qt.point(0,0), Qt.RightButton)
                }
            }
            

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved