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. How to reload an image in qml?
Forum Updated to NodeBB v4.3 + New Features

How to reload an image in qml?

Scheduled Pinned Locked Moved QML and Qt Quick
20 Posts 8 Posters 45.9k 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.
  • S Offline
    S Offline
    Satmosc
    wrote on last edited by
    #11

    I have an other question , I really never made a subclass of something displayable in QML.
    I know it should be something by QdeclarativeItem but I donot know how to implement a Simple Image Display !!!
    I tried to check if possible to use QLabel which is able to display image too, but I do not know how and where to begin for such a thing
    I have a list view which I have headache to reload images . even I am doing all possible changes , but it is not working perfect using signals and QML Image changes :(

    EDITed::::

    I just add following information
    ------------------------------- I had success by disabling Cache:false in QML image element.
    just do it and you can send a signal to QML image and force it to load again but by a trick I explain.

    Step by Step:

    1. Define a Subclass for your QML Window (if you did not already did it. it is easy.)
      it should be a class inherited from QDeclarativeView . . create a class using Qt Designer and call it mainwidnow (for example) and chose base class as 'QdeclarativeView' and set Type Information as 'QWidget' . finish and make the class.
      inside the constructor of the class , you need to define something like this:
      @rootContext()->setContextProperty("mainWindow",this);
      setSource(QUrl("qml/test1/mainWindow.qml"));
      @
      First line to define a context for current class to QML and second like is to assign a QML to current view.

    2. Define a Signal in QML here it is :: void imageChanged();

    3. in your QML image file define a Connections element

    @
    Image{
    id:imgContact ;smooth: true ; anchors.centerIn: parent;width:38;height: 38
    source: "image://roster/" + imageName
    cache: false

    Connections{
        target: mainWindow
        onImageChanged : {
            console.log("From QML....  Data Changed received " + index + "   "+imagename)
            imgContact.source="image://roster/reload/" + imagename
            imgContact.source="image://roster/" + imagename
        }
    }
    

    }
    @

    the connection receives Signals from C++ side rather than QML and the target is the context name defined in C++.
    the magic here are three part.
    a. cache:false to force qml image not to cache data
    b. change the source to force is to reload. I used /reload/ and inside my QDeclarativeImageprovider , I just remove this from id content how ? i explain here later.
    c. as soon as the image is changed the signal is emitted and the qml side (connections) receives it and it will modify imgContact source property.

    1. in the QDeclarative Image provider I used a QRegExp to compensate what I want from the extra data.
      you can define a regexp like '^reload/(.*)$' simplest model and the cap(1) of the regexp will be only the Id on the pixmap request (or image request).
      so you chose which one to load and you will always use id without the extra data.

    this is working perfect for me for now , i hope no other problem occurre. if so , I will report here

    M.Hatami
    To the Rationalism

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #12

      Start small. Get you custom item working on its own first before you worry about using it as a delegate in a ListView. Take a look at this "example":http://developer.qt.nokia.com/wiki/Busy_Indicator_for_QML for how to create a custom QML item in C++.

      Feel free to come back if anything is not clear.

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Satmosc
        wrote on last edited by
        #13

        @ZapB , thanks alot for info. I check it now :)

        M.Hatami
        To the Rationalism

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mario
          wrote on last edited by
          #14

          You could use a Timer (in QML) to have the image source property refreshed each 5 secs, for example:

          @
          Timer {
          interval: 5000
          repeat: true
          running: true
          onTriggered: { image.source = ""; image.source = "pic.png" }
          }
          @

          I think this should work

          S 1 Reply Last reply
          0
          • S Offline
            S Offline
            Satmosc
            wrote on last edited by
            #15

            @Mario. : we are not looking to refresh unexpectly, why should add some overhead to the program while it is not required.
            also , it is important to keep the cache : false

            M.Hatami
            To the Rationalism

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mario
              wrote on last edited by
              #16

              Well, you don't have to use a timer, you could simple just manipulate the property from any signal handler in the QML file:

              @
              image.source = "" ; image.source = "pic.png"
              @

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Amnell
                wrote on last edited by
                #17

                Hi,

                Instead of changing any property value, I suggest to call directly the NOTIFY signal of the QML property through QML, just like that :

                @Image {
                id: myImage
                source: "source.png"
                }

                onSomething: {
                myImage.sourceChanged(); // The NOTIFY signal of the source property
                }@

                Il will force a reload because (tell me if I'm wrong) QML property's are only updated due to a NOTIFY signal.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SteveKing
                  wrote on last edited by
                  #18

                  I think you might need to add a unique ID onto the end of the image name. QML has an image cache and you might find that "source.png" has been cached, so just telling the QML that the name has changed could result in a reload from the cache rather than from the image on disk.

                  Steve

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Amnell
                    wrote on last edited by
                    #19

                    [quote author="SteveKing" date="1314014784"]I think you might need to add a unique ID onto the end of the image name. QML has an image cache and you might find that "source.png" has been cached, so just telling the QML that the name has changed could result in a reload from the cache rather than from the image on disk.

                    Steve[/quote]

                    I've just tested it and you're right :

                    @Image {
                    source: "[...]/avatar.png"

                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            parent.sourceChanged(parent.source);
                            console.log("Try...");
                        }
                    }
                    

                    }@

                    It seems to reload from the cache. But if it's only a cache problem, we only have to wait for the "cache property":http://doc.qt.nokia.com/4.8/qml-image.html#cache-prop, coming with QtQuick 1.1.

                    EDIT : It needs an ID to work, like you said :

                    @Image {
                    source: "[...]/avatar.png"

                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            parent.source = "[...]/avatar.png?abc=" + Math.random();
                        }
                    }
                    

                    }@

                    But in this case, sending a new time the NOTIFY is useless.

                    1 Reply Last reply
                    0
                    • M mario

                      You could use a Timer (in QML) to have the image source property refreshed each 5 secs, for example:

                      @
                      Timer {
                      interval: 5000
                      repeat: true
                      running: true
                      onTriggered: { image.source = ""; image.source = "pic.png" }
                      }
                      @

                      I think this should work

                      S Offline
                      S Offline
                      saggy
                      wrote on last edited by
                      #20

                      @mario i have try this but i got error message ReferenceError: Can't find variable: image ....how to resolve this error

                      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