Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Images not switching



  • I'm designing something in QtQuick, and I have an image toggling effect that takes data based on data from a C++ backend. But for some reason, whenever I put in my switch statement, the images do not show up. Here is my code so far.

    Item{
        visible: true
        anchors.fill: parent
        Image{
            id:cylinderImages
            anchors.fill: parent
            fillMode: Image.PreserveAspectCrop
            source:{
                //Data is a C++ class and angle is a QString
                switch(Data.angle){
                     case "-10": return "/Images/C_-10.png"
                     case "-9": return "/Images/C_-9.png"
                     case "-8": return "/Images/C_-8.png"
                     ...
                     ...
                     ...
                     case "9": return "/Images/C_9.png"
                     case "10": return "/Images/C_10.png"
    

    It pops up with the error "Unable to assign [undefined] to QUrl" when I put it into a switch statement. I have another switch statement that has less cases but works completely fine. If anyone has any other way to switch these images that also works.


  • Moderators

    @GoldRatio try replacing the switch cases that are strings with actual numbers.
    ā€ž-10ā€œ to -10



  • @J-Hilk Data.angle is a string though. If I switch the switch cases to integers it won't work. I tried it
    just in case with integers and the same error of being "Unable to assign [undefined] to QUrl". I forgot to mention this but Data.angle is defined by a textEdit that sends it's information as a QString into a C++ backend.


  • Moderators

    @GoldRatio in c++ you can't use a 'string' as a switch argument. In JS you can however, I'm not sure on what QML will fall back to.

    have you tried with if else ?

    if(Data.angle == -10)
         return "/Images/C_-10.png"
    else if (Data.angle == -9)
         return "/Images/C_-9.png"
    
    //or
    if(Data.angle === "-10")
         return "/Images/C_-10.png"
    else if (Data.angle === "-9")
         return "/Images/C_-9.png"
    


  • @J-Hilk The switch statement is written in QML so I'm pretty sure it uses Javascript elements. I've tried to use it as an integer in the past changing the string data into an Int. It has always popped up with the same error and doesn't display any images.


  • Moderators

    @GoldRatio
    mmh ok,
    are you sure the img path is correct ? It is shown, when you remove the switch and set the source fixed to, for example, "/Images/C_-10.png" ?

    IIRC you would have to change that path to "qrc:/Images/C_-10.png" for it to be a valid source.



  • @GoldRatio

    Unlike C/C ++, JS supports strings in theexpression of a switch statement. Can you upload a simple source code to check for the error?



  • @J-Hilk The img path is correct because if I use the image path without the switch statement, it is able to be shown. However once I put it into the switch statement, I can't show the image and it pops up with the "Unable to assign" error.

    @Devopia53 Sorry I can't. There are certain things that prevent me from uploading files up onto here.



  • I've done a really basic fix that is pretty dumb. I just piled everything into a single QML file which makes the code very messy, but, it works :).


Log in to reply