Can slot be set in Javascript?



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



  • 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)
    }
    @



  • 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]



  • 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



  • 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



  • 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 ;)



  • Try if this works.

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



  • Great! It worked.
    Thanks, guys.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.