Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Creating a QML Image that will load different source if one source fails to load
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Creating a QML Image that will load different source if one source fails to load

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 16.3k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    bundickt
    wrote on 12 Jul 2011, 21:38 last edited by
    #1

    I am trying to create a QML Element that has three sources:

    source1: This is the first source that will be attempted to be loaded
    source2: If source1 is empty or it fails loading this source will be used
    sourceError: If source1 and source2 fail to load this one will be used.

    I have tried doing this with states, but get errors like
    <Unknown File>: QML StateGroup: Can't apply a state change as part of a state definition.

    I get that error when I change the state when the image status changes to Image.Error.

    So I tried just updating the source property. I am seeing two issues:

    1. If i do the following the status remains Image.Null
      @
      souce = "/img/path/does/not/exist/img.png"

    console.log("status " + (status == Image.Null)); // logs 'status true'
    @
    2. So I try setting the source on the "onStatusChanged" signal; if status == Image.Error change to the next source. This works with 2 source just fine, but when I add in the third source it only attempts to load the first two. I'm assuming it doesn't want to emit the onStatusChanged signal within an onStatusChanged signal.

    Has anyone tried to do this with success. Any hints would be great.


    I believe I could get this to work If I could add property changes to the end of the event queue ("AWT calls is that, not sure what qt calls is")

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on 12 Jul 2011, 21:57 last edited by
      #2

      When you say you've tried it using states, how did you try it?

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bundickt
        wrote on 12 Jul 2011, 22:46 last edited by
        #3

        Logic with states:

        I had 3 states:

        1. "source1":
        • changed the image source to source1 property
        1. "source2":
        • changed the image source to source2 property
        1. "sourceError"
        • changed the image source to sourceError property

        on status changed to Image.Error I check the current state and changed it next in line. For example, if the current state was "source1" I would change it to "source2". This would give me the following error:
        <Unknown File>: QML StateGroup: Can’t apply a state change as part of a state definition.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on 12 Jul 2011, 22:54 last edited by
          #4

          I was able to get it to work using the following (though I'm sure there are a lot better ways):

          @
          import QtQuick 1.0

          Rectangle {
          width: 360
          height: 360

          Image {
          id: img
          property string source1: "!red.png"
          property string source2: "!green.png"
          property string source3: "blue.png"

            property int curr: 1
          
            signal failedToLoad
          
            anchors.centerIn:  parent
          
            source: source1
          
            onStatusChanged: {
               console.log("Status changed to " + status);
               if (status == Image.Error)
               {
                  console.log("source: " + source + ": failed to load");
                  source = "";
                  failedToLoad()
               }
            }
          
            onFailedToLoad: {
               curr++;
            }
          
            onCurrChanged: {
               if      (curr == 1) { source = source1; }
               else if (curr == 2) { source = source2; }
               else if (curr == 3) { source = source3; }
               else { console.log("Cannot open"); }
            }
          

          }

          }
          @

          Quick and dirty, but it seems to work.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            baysmith
            wrote on 12 Jul 2011, 23:00 last edited by
            #5

            I haven't tested it, but maybe something like this?

            @
            Image {
            id: image
            property int errorCount: 0
            property string altSource
            property string errorSource

                onStatusChanged: {
                    if (image.status == Image.Error) {
                        errorCount += 1;
                        if (errorCount == 1)
                            image.source = image.altSource;
                        else
                            image.source = image.errorSource;
                    }
                }
            }
            

            @

            Nokia Certified Qt Specialist.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on 12 Jul 2011, 23:11 last edited by
              #6

              [quote author="Bradley" date="1310511624"]I haven't tested it, but maybe something like this?

              [/quote]

              That seemed to suffer from the same problem that he mentioned before. I think what's happening is that for some reason, if the status == Image.Error, and you try to load another image with an error, the status never changes from Image.Error, thus never triggering the onStatusChanged again. A workaround solution seems to be to reset the image source to "" in between load attempts, thus changing the status to Image.Null in the interim.

              I tweaked your code to the following:
              @
              Image {
              id: image
              property int errorCount: 0
              property string altSource
              property string errorSource

                 anchors.centerIn:  parent
              
                 onStatusChanged: {
                     if (image.status == Image.Error) {
                         errorCount += 1;
                         if (errorCount == 1) {
                             image.source = ""  // Gotta clear the source 
                             image.source = image.altSource;
                         } else {
                             image.source = image.errorSource;
                         }
                     }
                 }
              

              }
              @

              and it worked.

              Edit to add: And much more cleanly than my original kludgy workaround. :)

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              1
              • B Offline
                B Offline
                bundickt
                wrote on 13 Jul 2011, 00:46 last edited by
                #7

                That was the problem.

                Thanks for you help

                1 Reply Last reply
                0

                1/7

                12 Jul 2011, 21:38

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved