Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Images not switching
QtWS25 Last Chance

Images not switching

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.0k Views
  • 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.
  • G Offline
    G Offline
    GoldRatio
    wrote on last edited by
    #1

    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.

    J.HilkJ 1 Reply Last reply
    0
    • G GoldRatio

      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.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

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


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GoldRatio
        wrote on last edited by GoldRatio
        #3

        @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.

        J.HilkJ 1 Reply Last reply
        0
        • G GoldRatio

          @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.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @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"
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            GoldRatio
            wrote on last edited by
            #5

            @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.

            J.HilkJ D 2 Replies Last reply
            0
            • G GoldRatio

              @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.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @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.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • G GoldRatio

                @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.

                D Offline
                D Offline
                Devopia53
                wrote on last edited by
                #7

                @GoldRatio

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

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  GoldRatio
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    GoldRatio
                    wrote on last edited by
                    #9

                    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 :).

                    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