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. Connect to C++ signal from QML
Forum Updated to NodeBB v4.3 + New Features

Connect to C++ signal from QML

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 2.7k Views 3 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.
  • R Offline
    R Offline
    Rem-Kolomna
    wrote on last edited by
    #1

    Hi! I want to connect to C++ signals from QML like this:

    action.onMouseUp = function() {
        console.log("mouse up>>");
    }
    

    And declare in my C++ object this signal:

    signals:
      void mouseUp(const QPointF point);
    

    But I'm getting QML Error: TypeError: Cannot assign to read-only property "mouseUp". What might the problem be?

    p3c0P JKSHJ 2 Replies Last reply
    0
    • R Rem-Kolomna

      Hi! I want to connect to C++ signals from QML like this:

      action.onMouseUp = function() {
          console.log("mouse up>>");
      }
      

      And declare in my C++ object this signal:

      signals:
        void mouseUp(const QPointF point);
      

      But I'm getting QML Error: TypeError: Cannot assign to read-only property "mouseUp". What might the problem be?

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @Rem-Kolomna That is because you are trying to assign the function to that signal.
      Use Connections instead.

      Connections {
          target: action //if action is the target object
          onMouseUp: console.log(point)
      }
      

      157

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rem-Kolomna
        wrote on last edited by
        #3

        Yeah, connection is a way to go, but I want to simplify qml code and write it like that:

        action.onMouseUp = function()
        

        It is one line instead of four lines))) May be I can use Q_QROPERTY(QJSValue...) for this task?

        p3c0P 2 Replies Last reply
        0
        • R Rem-Kolomna

          Yeah, connection is a way to go, but I want to simplify qml code and write it like that:

          action.onMouseUp = function()
          

          It is one line instead of four lines))) May be I can use Q_QROPERTY(QJSValue...) for this task?

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @Rem-Kolomna You can't as mouseUp is a signal. You get something from signal and not give it.

          157

          1 Reply Last reply
          0
          • R Rem-Kolomna

            Yeah, connection is a way to go, but I want to simplify qml code and write it like that:

            action.onMouseUp = function()
            

            It is one line instead of four lines))) May be I can use Q_QROPERTY(QJSValue...) for this task?

            p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            @Rem-Kolomna You can also use a connect method. Eg:

            Component.onCompleted: {
                action.mouseUp.connect( function getPoint(myPoint) {console.log(myPoint) } );
            }
            

            157

            1 Reply Last reply
            0
            • R Rem-Kolomna

              Hi! I want to connect to C++ signals from QML like this:

              action.onMouseUp = function() {
                  console.log("mouse up>>");
              }
              

              And declare in my C++ object this signal:

              signals:
                void mouseUp(const QPointF point);
              

              But I'm getting QML Error: TypeError: Cannot assign to read-only property "mouseUp". What might the problem be?

              JKSHJ Online
              JKSHJ Online
              JKSH
              Moderators
              wrote on last edited by
              #6

              @Rem-Kolomna said:

              action.onMouseUp = function() {
                  console.log("mouse up>>");
              }
              

              Try replacing that with

              action.onMouseUp: {
                  console.log("mouse up>>");
              }
              

              @p3c0 said:

              @Rem-Kolomna You can't as mouseUp is a signal. You get something from signal and not give it.

              If mouseUp is the signal, then onMouseUp is the signal handler.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              p3c0P 1 Reply Last reply
              0
              • JKSHJ JKSH

                @Rem-Kolomna said:

                action.onMouseUp = function() {
                    console.log("mouse up>>");
                }
                

                Try replacing that with

                action.onMouseUp: {
                    console.log("mouse up>>");
                }
                

                @p3c0 said:

                @Rem-Kolomna You can't as mouseUp is a signal. You get something from signal and not give it.

                If mouseUp is the signal, then onMouseUp is the signal handler.

                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                @JKSH Right. I was just trying to explain that signals and the corresponding handlers cannot be assigned anything which produced that earlier error.

                157

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rem-Kolomna
                  wrote on last edited by
                  #8

                  I found a simple solution for my problem. I just declare a property

                  Q_PROPERTY(QJSValue onMouseUp READ onMouseUp WRITE setOnMouseUp)

                  that I can call from C++ side as QJSValue::call(). It allows me to write more clean code and ensure to have a single signal handler for this event (it's also important for me)

                  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