Image source property change from javascript



  • Hi, I didnt find anything what is useful especially for this question.

    I have an Image in a qml, and I have an other qml with a javascript code. How can I access the Image in an other qml and change its properties from javascript if a condition will be true?

    Have you any suggestion in this?
    Thank you!



  • Reference the id, and set the new source. Or, better yet, make the source URL a property of in your other QML file, and bind the source property of the image to that property.



  • I am new to Qt and cant imagine sure what Ive to do.
    Could you write an example please?



  • For the example off course I think only that few essential rows which is needed for me in this problem.



  • Something along these lines:

    Other.qml:
    @
    Item {
    id: myOtherComponent;
    property string imageSource;

    // your javascript will modify the imageSource property when appropriate
    }
    @

    main.qml:
    @
    Item {
    Other {
    id: myMagicJavaScriptStuff;
    }

    Image {
    id: theImage;
    source: myMagicJavaScriptStuff.imageSource;
    }
    }
    @

    Something along those lines would work...



  • Thank you! I'll test it as soon as I can!



  • The above code didnt work for me. But I had an idea and made a cute code. Please take a look to the following part of qml:
    @
    Image {
    source: getOutImgSource()

    function getOutImgSource() {
    if (loggedIn) return "images/menu-08Loged.png";
    else return "images/menu-08.png";
    }
    }
    @

    But it doesnt work to! It looks like the qmlviewer doesnt refresh the graphics. Is it possible?

    Have you any idea?

    Edit: please use @ tags around code sections to format them; Andre


Log in to reply
 

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