Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to pass mouse events from qml file to c++?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
13 Posts 2 Posters 3.1k Views
  • 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
    JennyAug13
    wrote on last edited by JennyAug13
    #2

    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*
    
    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #3

      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
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      J 1 Reply Last reply
      0
      • dheerendraD dheerendra

        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);
        
        J Offline
        J Offline
        JennyAug13
        wrote on last edited by
        #4

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

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JennyAug13
          wrote on last edited by
          #5

          I couldnt do this as follows

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

          Iam getting same error

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #6

            hmmmm.
            Please define the method like follows

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

            Then call this method from QML side.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            J 1 Reply Last reply
            1
            • dheerendraD dheerendra

              hmmmm.
              Please define the method like follows

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

              Then call this method from QML side.

              J Offline
              J Offline
              JennyAug13
              wrote on last edited by
              #7

              @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.

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by dheerendra
                #8

                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
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                J 1 Reply Last reply
                1
                • dheerendraD dheerendra

                  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.

                  J Offline
                  J Offline
                  JennyAug13
                  wrote on last edited by
                  #9

                  @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).

                  1 Reply Last reply
                  0
                  • dheerendraD Offline
                    dheerendraD Offline
                    dheerendra
                    Qt Champions 2022
                    wrote on last edited by
                    #10

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

                    Dheerendra
                    @Community Service
                    Certified Qt Specialist
                    http://www.pthinks.com

                    J 1 Reply Last reply
                    0
                    • dheerendraD dheerendra

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

                      J Offline
                      J Offline
                      JennyAug13
                      wrote on last edited by JennyAug13
                      #11

                      @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?

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        JennyAug13
                        wrote on last edited by JennyAug13
                        #12

                        Yes, understood,

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

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

                        Sorry for silly doubt.

                        1 Reply Last reply
                        0
                        • dheerendraD Offline
                          dheerendraD Offline
                          dheerendra
                          Qt Champions 2022
                          wrote on last edited by
                          #13

                          Hopefully this is resolved. Please move the issue to solved

                          Dheerendra
                          @Community Service
                          Certified Qt Specialist
                          http://www.pthinks.com

                          1 Reply Last reply
                          0

                          • Login

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