Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to pass mouse events from qml file to c++?



  • I have my qml file and when user pressed the mouse on the button, i have to pass the mouse event to my c++ file. I found some examples on internet using QEvent etc. but they aren't complete enough. Would be great if some source of example is shared. Thanks in advance.



  • I did in the same way but i am getting the following error:

    http://doc.qt.io/qt-5/qfileopenevent.html

    Here is my function

    bool test::passMouseEvents(QEvent *event)
    {
        qDebug()<< "Button pressed event is captured and sent to test.cpp";
    
        if(event->type() == QEvent::MouseButtonPress)
        {
           
            return true;
        }
        else
        {
            qDebug() << "no mouse events";
            return false;
        }
    }
    

    Inside my qml file, i have following code

    test{
            id: demo
        }
    
    demo.passMouseEvents(mouse);
    

    But when i run my code, i am getting following error:

    Error: Unknown method parameter type: QEvent*
    

  • Qt Champions 2017

    You can't do this. MouseEvent does not map to QMouseEvent at C++ side. Which parameters you need at C++ side ? x, y ? Just write a slot and pass the required arguments e.g

    demo.passMouseEvents(mouse.x,mouse.y);
    


  • @dheerendra i tried giving mouseX, and then even it shows the same error



  • I couldnt do this as follows

    demo.passMouseEvents(mouse.x,mouse.y);
    

    Iam getting same error


  • Qt Champions 2017

    hmmmm.
    Please define the method like follows

    Q_INVOKABLE void test::takeInputs(int x, int y);

    Then call this method from QML side.



  • @dheerendra Yes, this is working, understood. Now i want to pass those events to another object in QML from c++. If you dont mind, can i know how can it be done? Thanks in advance.


  • Qt Champions 2017

    From c+++ emit signal & handle this signal in qml side. From handler pass the same to qml object. As side note I am not sure what is this round about you are doing.



  • @dheerendra In my qml file, there are two sliding buttons and when they overlap, and when a mouse event takes place, only my upper button will get the mouse events and not the lower button since upper button is painted over the lower button.

    I want to capture mouse event from one object(say upper button) in my qml file and pass the same mouse events to two objects(say upper button and also lower button) on some codition check like, when the buttons are overlapped and if the user pressed mouse event is on the left half of the button, the mouse event has to be passed to lower button also otherwise if the mouse events are on the right half of the button, the mouse event has just to be passed to upper button only.

    When I tried entirely from qml side, it didnt work. So i want to create a c++ class and from there, i want to send the mouse events to the both objects (upper button and lower button inside my qml file).


  • Qt Champions 2017

    Did you try the way I explained signal from c++ side ? Does it work ?



  • @dheerendra
    Yes, it worked. But came across another error.
    Actually i am half way sir. I am facing little problem while implementing.

    I am emitting a signal in my c++ file inside a condition check for button overlapping. Now i am trying to handle the signal which i have emitted inside my c++ file from my qml file inside the lower button element. Since i cannot call my signal handler directly, I have taken Mouse area class as follows

    test{
            id: demo1
        }
    
    rangeButton {
            id: lowerValMarker              //make lowerButton color bit transperant
            opacity: 0.9
            anchors.bottom: parent.bottom
            property bool __blockPosUpdate: false
            enabled: parent.enabled
            width:  buttonSize
            height: buttonSize
            offset: -buttonSize / 2
            posMin: calcPosLowerMin()               //Calculating the lowerButton position
            posMax: calcPosLowerMax()
            // Add a small value to make sure we rather slightly exceed the threshold
            incStep: val2pos(rangeControl.stepSize + 0.00001)
            decStep: val2pos(rangeControl.stepSize + 0.00001)
            MouseArea{
                id:mouseArea
                anchors.fill: parent
                demo1.onMouseEventSignal: {
                        .....................
                }
            }
    

    But i am facing following error

    Invalid property name demo1
    

    How can i handle my signal emitted in my c++ file inside my qml file?



  • Yes, understood,

    inside my qml file, instead of mouseArea class it should be like

            test{
                onMouseEventSignal:
                    console.log("hello")
            }
    

    Sorry for silly doubt.


  • Qt Champions 2017

    Hopefully this is resolved. Please move the issue to solved


Log in to reply