Results 1 to 7 of 7

Thread: [Qt4] How to load Url image into QImage?

  1. #1
    Join Date
    Jan 2006
    Posts
    8
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question [Qt4] How to load Url image into QImage?

    Hi to all ,

    I want to load an URL image , for example ,
    http://mydomain.com/images/image.jpg , into a QImage.

    I try some things , but no one works :

    Qt Code:
    1. QUrl url("http://mydomain.com/images/image.jpg");
    2. QImage image(url.toLocalFile());
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. QHttp url("http://mydomain.com/images/image.jpg");
    2. QImage image;
    3. image.loadFromData(url.readAll());
    To copy to clipboard, switch view to plain text mode 

    How to do this?

  2. #2
    Join Date
    Feb 2006
    Location
    Boulder, Colorado, USA
    Posts
    63
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4] How to load Url image into QImage?

    I'm not too familar with qt's networking classes, but a quick scan of the docs shows that the constructor sets the host, but not the whole url. For that you need to use the get method.

    See http://doc.trolltech.com/4.1/qhttp.html#get

  3. #3
    Join Date
    Jan 2006
    Posts
    8
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4] How to load Url image into QImage?

    Done !!!.

    Qt Code:
    1. QUrl url("http://mydomain.com/images/image.jpg");
    2. http = new QHttp(this);
    3. connect(http, SIGNAL(requestFinished(int, bool)),this, SLOT(Finished(int,
    4. bool)));
    5. buffer = new QBuffer(&bytes);
    6. buffer->open(QIODevice::WriteOnly);
    7. http->setHost(url.host());
    8. Request=http->get (url.path(),buffer);
    9.  
    10. public slots:
    11.  
    12. void Finished(int requestId, bool error){
    13. if (Request=requestId){
    14. QImage img;
    15. img.loadFromData(bytes);
    16. }
    17.  
    18. private :
    19.  
    20. QBuffer *buffer;
    21. QByteArray bytes;
    22. QHttp *http;
    23. int Request;
    To copy to clipboard, switch view to plain text mode 

  4. The following 2 users say thank you to Gonzalez for this useful post:

    antweb (11th October 2014), jamo_ (18th April 2011)

  5. #4
    Join Date
    Jul 2014
    Posts
    46
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: [Qt4] How to load Url image into QImage?

    Hello,

    I made use of your code and I am able to access Url images. The issue that i am facing is that I am not able to access all Url images. It works for some images but not for all?
    Do you know what might be the issue?

    Like for eg.
    I am able to acquire this image : http://www.slowstop.com/images/uploads/YouTube_Logo.jpg
    But not this one : http://colorlib.com/dazzling/wp-cont...nt-150x150.jpg
    These are two random pics from google images.

    Please help me out!

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: [Qt4] How to load Url image into QImage?

    QHttp has been deprecated for a few years now. You should rewrite your code using QNetworkRequest and related classes. There is at least one HTTP download example in the docs.

    Start with a small, self-contained program that only does this one task. If your code still does not behave then please post it here and explain what the problem is.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  7. #6
    Join Date
    Jul 2014
    Posts
    46
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: [Qt4] How to load Url image into QImage?

    I have a pretty straight forward and simple program. I have made use of QNetworkRequest this time.

    .cpp code :
    Qt Code:
    1. #include "http_new.h"
    2. #include <QNetworkAccessManager>
    3. #include <QUrl>
    4. #include <QNetworkRequest>
    5. #include <QAuthenticator>
    6. #include <QNetworkReply>
    7. #include <QtCore/QCoreApplication>
    8. #include <QDebug>
    9. #include <QImageReader>
    10.  
    11. QNetworkAccessManager* nam;
    12.  
    13. http_new::http_new(QWidget *parent, Qt::WFlags flags)
    14. : QMainWindow(parent, flags)
    15. {
    16. ui.setupUi(this);
    17.  
    18. nam = new QNetworkAccessManager(this);
    19. QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
    20. this, SLOT(finishedSlot(QNetworkReply*)));
    21.  
    22. QUrl url("http://www.slowstop.com/images/uploads/YouTube_Logo.jpg");
    23. QNetworkReply* reply = nam->get(QNetworkRequest(url));
    24. }
    25.  
    26. http_new::~http_new()
    27. {
    28.  
    29. }
    30.  
    31. void http_new::finishedSlot(QNetworkReply *reply)
    32. {
    33. // Reading attributes of the reply
    34. // e.g. the HTTP status code
    35. QVariant statusCodeV =
    36. reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    37.  
    38. // Or the target URL if it was a redirect:
    39. QVariant redirectionTargetUrl =
    40. reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    41.  
    42. // no error received?
    43. if (reply->error() == QNetworkReply::NoError)
    44. {
    45. // read data from QNetworkReply here
    46. QByteArray bytes = reply->readAll(); // bytes
    47. QPixmap pixmap;
    48. pixmap.loadFromData(bytes);
    49. ui.label->setPixmap(pixmap);
    50. }
    51. // Some http error received
    52. else
    53. {
    54. // handle errors here
    55. }
    56.  
    57. // We receive ownership of the reply object
    58. // and therefore need to handle deletion.
    59. delete reply;
    60. }
    To copy to clipboard, switch view to plain text mode 

    .h code :
    Qt Code:
    1. #ifndef HTTP_NEW_H
    2. #define HTTP_NEW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QImage>
    6. #include <QPixmap>
    7. #include <QPainter>
    8. #include <QColor>
    9. #include <QNetworkAccessManager>
    10. #include <QUrl>
    11. #include <QBuffer>
    12. #include <QHttp>
    13. #include <QNetworkRequest>
    14. #include <QNetworkReply>
    15. #include "ui_http_new.h"
    16.  
    17. class http_new : public QMainWindow
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. http_new(QWidget *parent = 0, Qt::WFlags flags = 0);
    23. ~http_new();
    24.  
    25. public slots:
    26. void finishedSlot(QNetworkReply* reply);
    27.  
    28. private:
    29. Ui::http_newClass ui;
    30. QImage * image;
    31. QPixmap screenImage;
    32. QPainter * p;
    33. QBuffer *buffer;
    34. QByteArray bytes;
    35. QHttp *http;
    36. int Request;
    37. };
    38.  
    39. #endif // HTTP_NEW_H
    To copy to clipboard, switch view to plain text mode 

    main code:
    Qt Code:
    1. #include "http_new.h"
    2. #include <QtGui/QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. http_new w;
    8. w.showMaximized();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    I am still facing the same issue as i had written earlier.
    The URL i have used right now. I am able to view that image in my Label but that is not happening for every image link.
    I really cannot understand why?
    The second link that I have provided earlier is a working image link through browser but does not give me any image on my Label.

    Please Help.

  8. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4] How to load Url image into QImage?

    Have you checked where the problem is?
    Do you get no data or does the pixmap loading fail?

    Cheers,
    _

Similar Threads

  1. get QImage object from buffer
    By Sheng in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2012, 03:03
  2. Is there a better way to load a big image?
    By learning_qt in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2008, 16:00
  3. Multiple QPainters on QImage
    By fire in forum Qt Programming
    Replies: 3
    Last Post: 14th August 2008, 14:10
  4. best way to load image from db
    By C167 in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2008, 19:24
  5. how to load a Qt non-supported Image format
    By Nithya in forum Qt Programming
    Replies: 5
    Last Post: 3rd April 2008, 13:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.