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. Can slot be set in Javascript?
Forum Updated to NodeBB v4.3 + New Features

Can slot be set in Javascript?

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 3 Posters 4.4k Views 1 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.
  • L Offline
    L Offline
    Landy
    wrote on last edited by
    #1

    Hi all,

    Here is a question, can slot be set in Javascript?
    Code:
    @ Rectangle {
    id: rootRect

        width: 100
        height: 100
    
        Rectangle {
            id: subRect
    
            width: 50
            height: 50
        }
        Component.onCompleted: {
            //something like the following code
            /*
              subRect.onWidthChanged = function () {
              }
              */
        }
    }@
    

    Take the code for example, i wanna set subRect's onWidthChanged slot in rootRect's Component.onCompleted, is there any solution for it?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MichK
      wrote on last edited by
      #2

      onWidthChanged is a signal emmited when width property of your subRect changes.
      If you want to call function of a qml element after width has changed then you have to do sth like this
      or do a property binding.
      @
      Component.onCompleted: {
      subRect.onWidthChanged.connect(otherComponent.doSthing)
      }
      @

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Landy
        wrote on last edited by
        #3

        Hi michal,

        I tried as your suggestion.
        but it didn't work.
        Here is an error information:
        TypeError: Result of expression 'subRect.onWidthChanged' [undefined] is not an object.
        So, I changed the code as following:
        @ function dosth() {
        console.log("this is do sth");
        }

        Component.onCompleted: {
            root.widthChanged.connect(dosth());
        }@
        

        There is no error after I modified.
        But there is another problem:
        The function "dosth()" is called only once, only when the application starts.
        after that, however I change the width, dosth() will not be called any more.
        Do u have any idea?

        [quote author="michal.k" date="1313734376"]onWidthChanged is a signal emmited when width property of your subRect changes.
        If you want to call function of a qml element after width has changed then you have to do sth like this
        or do a property binding.
        @
        Component.onCompleted: {
        subRect.onWidthChanged.connect(otherComponent.doSthing)
        }
        @[/quote]

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MichK
          wrote on last edited by
          #4

          On the other hand maybe you should write something like this:
          @
          Rectangle {
          id: rootRect

          width: 100
          height: 100
          
          function writeMe() {
              console.log("subRectWidth changed")
          }
          
          Rectangle {
              id: subRect
          
              width: 50
              height: 50
              onWidthChanged: writeMe()
          }
          Component.onCompleted: {
              subRect.width = 100
          }
          

          }
          @
          But it all depends what you want to do.
          writeMe will be called only when width of subRect changes

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Landy
            wrote on last edited by
            #5

            Yes, I know that we can use signal like that.
            But, I wanna know if we can connect a slot and a signal in javascript.
            For example:
            If I create an Rectangle object named "rect" dynamic, and I wanna do sth when rect's width changed.
            When the object is static, of course I can implement it very easily. But I don't know how to do it when the object is created dynamically

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MichK
              wrote on last edited by
              #6

              Aha, hmm so you mean situation where you create object like this:
              @
              var rectItem = Qt.createQmlObject('import Qt 4.7; Rect { id: subRect'
              + '}',
              parentObject);
              @
              I don't know how to do it in this kind of situation ;)

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Diph
                wrote on last edited by
                #7

                Try if this works.

                @Connections {
                target: rectItem
                onWidthChanged: console.log("width " + width)
                }@

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  Landy
                  wrote on last edited by
                  #8

                  Great! It worked.
                  Thanks, guys.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    Landy
                    wrote on last edited by
                    #9

                    I am sorry to open this thread again.
                    Thanks for Diph's solution, now I can handle signals out of their senders.
                    But I met a new problem again.
                    I found that I couldn't handle a signal dynamically with Diph's solution.
                    So, I still want to know if there is a way to handle signal dynamically.
                    Any guy has a solution about this?

                    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