Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to reload an image in qml?
Qt 6.12 LTS is out! See what's new in the release blog

How to reload an image in qml?

Scheduled Pinned Locked Moved QML and Qt Quick
20 Posts 8 Posters 51.1k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #7

    Cool. Glad to see you got it working.

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

      I see that what I was searching was a bit explained here. but I should tell that it is easy at all.

      If you have an Image Element in QML, you can define a Connection inside Image element.
      then in the Connection Element , specify the target equal your C++ Class which must be the context that is inherited from QDeclarative... View , then call on<Signal Name with first letter Capital>

      I am using Image Provider by QDeclarative Image Provider.
      @
      Image{
      id:imgContact ;smooth: true ; anchors.centerIn: parent;width:30;height: 30
      source: "image://roster/" + jid //"../images/unknownContact_30x30.png"
      Connections{
      target: mainWindow
      onRostermodelContactImageChanged : console.log("From QML.... Data Changed received ")
      }
      }
      @

      it is easy....
      but I am looking for a suite able way to reload the Image in QML from source rather than changing a property.

      M.Hatami
      To the Rationalism

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #9

        Another approach would be to create your own MyImage custom C++ type and export that to QML. Then your custom type give it a slot that you can call to refresh the image from source even if the source filename has not changed.

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

          [quote author="ZapB" date="1309766171"]Another approach would be to create your own MyImage custom C++ type and export that to QML. Then your custom type give it a slot that you can call to refresh the image from source even if the source filename has not changed.[/quote]

          It looks your recommendation should be much better than QML Image. because QML image resize is also not happening all times . it is randomly and I cannot understand why ! there is something about it that I do not know and I cannot fix it.

          I have a question please.
          If I use Image Element from Qt ImageViewer , then how is the performance ?
          I need to use it as an image element in ListView ? will it cause a performance issue ?

          M.Hatami
          To the Rationalism

          1 Reply Last reply
          0
          • 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
            • ? Offline
              ? Offline
              A Former User
              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.

              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
                • ? Offline
                  ? Offline
                  A Former User
                  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
                    • ? Offline
                      ? Offline
                      A Former User
                      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
                      • ? Offline
                        ? Offline
                        A Former User
                        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
                        • ? Offline
                          ? Offline
                          A Former User
                          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
                          • ? Offline
                            ? Offline
                            A Former User
                            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
                            • ? A Former User

                              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
                              • Unsolved