[SOLVED][JS] id in parameter



  • Hi,

    I have main.qml.
    @import Qt 4.7

    Item {
    id: topMiddlePane

    Image {
        id: buttonClasse
        source: "pict/button1Off.png"
        x: 329
        y: 0
         width: 123
         height: 52
        opacity: 1
    
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            onClicked: topMiddlePane.onPage("button1");
        }
    }
    Image {
        id: button2
        source: "pict/button2Off.png"
        x: 210
        y: 0
         width: 111
         height: 52
        opacity: 1
    
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            onClicked: topMiddlePane.onPage("button2");
        }
    
    
    
    function onPage(id){
        id.source = "pict/"+id+"On.png";
    }
    

    }@

    I want to switch source according to the value of my parameter (to onPage function).
    But my problem is that QML seeks id.source and not bouton1.source
    Do you have a solution?

    sorry for my English



  • Did you try: @onClicked: topMiddlePane.onPage(button2);@



  • Great but now the id in the chain displays the error "QDeclarativeImage(0x31326e8)On.png"



  • That's because the id is not a string nor a regular variable.
    [quote]The id value is a special value for a QML object and should not be thought of as an ordinary object property; [/quote] "link to doc":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeintroduction.html#object-identifiers

    What might be a solution:
    @function onPage(id){
    id.source = (id == button1)? "pict/button1On.png": "pict/button2On.png";
    }@



  • Yes but i have 20 buttons I can not make 20 times the process.

    Do you have a other solution? I may not be the right idea



  • Or else:
    @Image {
    id: button2
    property string name: "button2"
    source: "pict/button2Off.png"
    x: 210
    y: 0
    width: 111
    height: 52
    opacity: 1

        MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                onClicked: topMiddlePane.onPage(id);
        } 
    
        function onPage(id){
                id.source = "pict/"+id.name+"On.png";
        }
    

    }@



  • Good i love that.

    Thx you and french kiss



  • Pas de souci ;-)



  • If it solved your issue, please don't forget to prepend [Solved] to the topic title.



  • You could also use the objectName property.


Log in to reply
 

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