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 show QImage from http url?
Forum Updated to NodeBB v4.3 + New Features

How to show QImage from http url?

Scheduled Pinned Locked Moved QML and Qt Quick
27 Posts 4 Posters 19.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.
  • D Offline
    D Offline
    DenisKormalev
    wrote on last edited by
    #14

    "example about QDeclarativeNetworkAccessManagerFactory":http://doc.qt.nokia.com/4.7/declarative-cppextensions-networkaccessmanagerfactory.html
    "QNetworkDiskCache":http://doc.qt.nokia.com/4.7/qnetworkdiskcache.html (there is an example of how to use it with QNAM)

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jkosonen
      wrote on last edited by
      #15

      [quote author="Denis Kormalev" date="1311066630"]"example about QDeclarativeNetworkAccessManagerFactory":http://doc.qt.nokia.com/4.7/declarative-cppextensions-networkaccessmanagerfactory.html
      "QNetworkDiskCache":http://doc.qt.nokia.com/4.7/qnetworkdiskcache.html (there is an example of how to use it with QNAM)[/quote]

      Thanks, will try that!

      but there is this:

      When sending requests, to control the preference of when to use the cache and when to use the network, consider the following:

      @ // do a normal request (preferred from network, as this is the default)
      QNetworkRequest request(QUrl(QString("http://qt.nokia.com")));
      manager->get(request);

      // do a request preferred from cache
      QNetworkRequest request2(QUrl(QString("http://qt.nokia.com")));
      request2.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
      manager->get(request2);@

      How do I say in QML to prfer cache?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DenisKormalev
        wrote on last edited by
        #16

        jkosonen, it will prefer a cache in most part of cases (if we are talking about images)

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jkosonen
          wrote on last edited by
          #17

          [quote author="Denis Kormalev" date="1311067186"]jkosonen, it will prefer a cache in most part of cases (if we are talking about images)[/quote]

          OK, but how to say then not to use cache, or clear cache?
          Cache isn't always preferred, would be nice to know when it is used and when it's not.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DenisKormalev
            wrote on last edited by
            #18

            jkosonen, clearing cache is possbile with simple removing all contents of cache dir. I'm not aware of any other ways.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chuck Gao
              wrote on last edited by
              #19

              You can try Qt 4.8, it has a better image caching for QML. Hope it will work for you :D

              Chuck

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jkosonen
                wrote on last edited by
                #20

                Can anyone help with the question in the first post, how that could be done?

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #21

                  [quote author="jkosonen" date="1312700928"]Can anyone help with the question in the first post, how that could be done?[/quote]

                  By using [[Doc:QNetworkAccessManager]] - there are numerous examples out there (even in this forums :-) ) that show you the basic path.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jkosonen
                    wrote on last edited by
                    #22

                    Don't think so that there is... and what a useless post..

                    [quote author="Volker" date="1312717450"]
                    [quote author="jkosonen" date="1312700928"]Can anyone help with the question in the first post, how that could be done?[/quote]

                    By using [[Doc:QNetworkAccessManager]] - there are numerous examples out there (even in this forums :-) ) that show you the basic path.[/quote]

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #23

                      So another useless post with another "link":http://developer.qt.nokia.com/wiki/Download_Data_from_URL, maybe of help this time.

                      I'm so awfully sorry to have put you on the wrong track. Of course it's not in the forums, but in the wiki. Silly me.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jkosonen
                        wrote on last edited by
                        #24

                        [quote author="Volker" date="1312729123"]So another useless post with another "link":http://developer.qt.nokia.com/wiki/Download_Data_from_URL, maybe of help this time.

                        I'm so awfully sorry to have put you on the wrong track. Of course it's not in the forums, but in the wiki. Silly me.[/quote]

                        That still doesn't really help in the original guestion, how I can return the Image trough slot..

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #25

                          I don't get your question.

                          This does not work?

                          @
                          QImage img;
                          // fill the image with data
                          return img;
                          @

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            jkosonen
                            wrote on last edited by
                            #26

                            [quote author="Volker" date="1312731557"]I don't get your question.

                            This does not work?

                            @
                            QImage img;
                            // fill the image with data
                            return img;
                            @

                            [/quote]

                            @

                            QImage requestImage(const QString &id;, QSize *size, const QSize &requestedSize;)
                            {

                                 QDir dir;
                                 QFile file( dir.absolutePath() + "images/" + id + ".jpg" );
                            
                                 if (file.exists())
                                 {
                                    QImage img;
                                    img.load(dir.absolutePath() + "images/" + id + ".jpg");
                                    return img;
                                 }
                                 else {
                            
                                    Load from url
                            
                                 }
                            

                            }
                            @

                            Don't know how I can fill the image inside the else slot. Maybe I just don't understand something.

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              goetz
                              wrote on last edited by
                              #27

                              Download the image, e.g. by using the QNetworkAccessManager download example mentioned in the wiki. You may want to extend it to pass the desired path of the file. The helper class saves the downloaded file to a file, then just continue like it's already there. I would change the logic a bit:

                              @

                              QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize)
                              {
                              QDir dir;
                                  QFileInfo fi( dir.absolutePath() + "images/" + id + ".jpg" );
                              if(!fi.exists()) {
                              // download the image
                              // using the helper class
                              // it saves the image to the path
                              // denoted by file
                              // pass the file path with
                              // fi.absoluteFilePath()
                              }
                              return QImage(fi.absoluteFilePath());
                              }
                              @

                              The code is the same once you actually have the image data. So just check if it's there, download it if not. After that it is "in the cache".

                              Oh, and of course, in the download helper or the if part, make sure you actually got an image from the server and check for HTTP in the download helper. The server may be down or there might be another error on the network part - people are pulling plugs from time to time :-)

                              EDIT:
                              there was some copy'n'paste garbage in the code, so make sure to copy it from the forums, not the notification email.

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              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