Results 1 to 4 of 4

Thread: Problem getting an image from a web server

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

    Default Problem getting an image from a web server

    I want to be able to get an image from a web server. Any image, any size, any web server.

    The following code compiles with no warning or problems at all:

    Qt Code:
    1. #include <QHttp>
    2. #include <QHttpRequestHeader>
    3. #include <QHttpResponseHeader>
    4. #include <QByteArray>
    5. #include <QLabel>
    6. #include <QImage>
    7. #include <QString>
    8. #include <QTextBrowser>
    9. #include <iostream>
    10.  
    11. #include "image.h"
    12. using namespace std;
    13.  
    14. Image::Image(){
    15. tb = new QTextBrowser(this);
    16. dataReceived = new QByteArray();
    17. dataReceived->clear();
    18. label = new QLabel(this);
    19. http = new QHttp("localhost", 80, this);
    20. QHttpRequestHeader header("GET", "/images/test.jpg");
    21. header.setValue("Host", "localhost");
    22. http->setHost("localhost");
    23. http->request(header);
    24.  
    25. connect(http, SIGNAL(readyRead(const QHttpResponseHeader &)), this, SLOT(getLogo(const QHttpResponseHeader &)));
    26. connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(requestFinished(int, bool)));
    27. //connect(http, SIGNAL(done(bool)), this, SLOT(done(bool)));
    28.  
    29. setFixedSize(300, 150);
    30. setCentralWidget(label);
    31. show();
    32. }
    33.  
    34. void Image::getLogo(const QHttpResponseHeader & resp){
    35. //QByteArray imageData(http->readAll());
    36. //QImage image(imageData.data());
    37. //QImage image("logo.png");
    38. //label->setPixmap(QPixmap::fromImage(image));
    39.  
    40. cout << "The response:\n";
    41. dataReceived->append(http->readAll());
    42. cout << "dataReceived lenght: " << dataReceived->size() << endl;
    43. //QString temp1 = dataReceived->data();
    44. //cout << temp1.toStdString();
    45. }
    46.  
    47. void Image::requestFinished(int n, bool error){
    48. if(error){
    49. cout << "requestFinished error\n";
    50. }
    51. else{
    52. if(dataReceived->size()==0){
    53. return;
    54. }
    55. cout << "requestFinished ok\n";
    56. cout << "Image lenght: " << dataReceived->size() << endl;
    57. site.append("<html><tr><td>a");
    58. site.append(dataReceived->data());
    59. site.append("</td></tr></html>");
    60. //tb->append(dataReceived->data());
    61. tb->append(site);
    62. //pixmap.loadFromData(dataReceived->data());
    63. //label->setPixmap(pixmap);
    64.  
    65. }
    66.  
    67. }
    68.  
    69. void Image::done(bool error){
    70. cout << "DONE function, bool reported:\n";
    71. if(!error)
    72. cout << "no error\n";
    73. pixmap.loadFromData(dataReceived->data());
    74. label->setPixmap(pixmap);
    75. tb->append(dataReceived->data());
    76. }
    To copy to clipboard, switch view to plain text mode 


    For some reason I always get a corrupted image. The console also prints a message liske this:

    The response:
    dataReceived lenght: 1334
    requestFinished ok
    Image lenght: 1334
    Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
    JPEG datastream contains no image

    What is wrong ?

  2. #2
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Problem getting an image from a web server

    What's inside of dataReceived? Is it valid html or binary data? What's the size of test.jpg? Can you access test.jpg with a regular browser?
    Software Engineer



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

    Default Re: Problem getting an image from a web server

    Yes, I can access the image from a regular browser.

    The size of the image in my local directory is exactly the same size of the one reported in the console.

    Inside the QByteArray I keep all the data I received from the QHttp response, so my guess is that it is binary data.

  4. #4
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem getting an image from a web server

    Quote Originally Posted by probine View Post
    What is wrong ?
    That is a very good question ;-)


    I sadly do not see what type "site" is. But it could be that you are bitten by an implicit conversion of QByteArray to QString (including the complete messing up of any binary data in it) in the lines:
    Qt Code:
    1. site.append("<html><tr><td>a");
    2. site.append(dataReceived->data());
    3. site.append("</td></tr></html>");
    To copy to clipboard, switch view to plain text mode 

    What parameter type does site.append() accept?
    (Since I do hate these kind of errors, I have always QT_NO_CAST_FROM_ASCII defined. It does take some work to wrap everything in QLatin1String and friends though...)

    I see you have different trials in your code commented out.
    What happend when you had these lines in:
    Qt Code:
    1. //pixmap.loadFromData(dataReceived->data());
    2. //label->setPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 


    Have a nice day :-)

Similar Threads

  1. How a server can write "Hello" to a browser ?
    By probine in forum Qt Programming
    Replies: 2
    Last Post: 1st December 2006, 14:43
  2. Transfer image problem
    By probine in forum Qt Programming
    Replies: 7
    Last Post: 4th May 2006, 10:24
  3. problem with the back ground image
    By Seema Rao in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2006, 21:34
  4. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36
  5. problem with image colections
    By zlatko in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2006, 13:43

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.