Results 1 to 15 of 15

Thread: Diplay web image

  1. #1
    Join Date
    Oct 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Diplay web image

    Hi all,

    I want to display a web image into a QLabel.
    this is my code :

    Qt Code:
    1. void Flux::replyFinished(QNetworkReply* r)
    2. {
    3. /* Récuperation d'un XML et construction du QUrl url_pochette */
    4.  
    5. QUrl *url_pochette = new QUrl;
    6. url_pochette->setScheme("http");
    7. url_pochette->setHost("host.net");
    8. url_pochette->setPath("/pochette/image.jpg");
    9.  
    10. r->deleteLater();
    11.  
    12. QObject::connect(http, SIGNAL(finished(QNetworkReply*)), this, SLOT(load_pochette(QNetworkReply*)));
    13. QNetworkRequest request(*url_pochette);
    14. http->get(request);
    15. }
    16.  
    17. void Flux::load_pochette(QNetworkReply* r)
    18. {
    19. QImage img;
    20. if (img.load(r, "JPEG"))
    21. {
    22. p->image_pochette->hide();
    23. p->image_pochette->setPixmap(QPixmap::fromImage(img));
    24. p->image_pochette->show();
    25. }
    26. else
    27. cout << "Impossible de charger l'image" << endl;
    28. }
    To copy to clipboard, switch view to plain text mode 

    But "Impossible de charger l'image" is display.

    Sorry for my english, i'm french.

    Thanks for reply.
    Last edited by wysota; 25th October 2009 at 20:23.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Diplay web image

    I would be nice to at least check if the request ended successfully by checking the status code of the reply.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Diplay web image

    I add this line :

    if (r->error() == QNetworkReply::NoError) cout << "No error" << endl;

    and "no error" is display!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Diplay web image

    That's not what I meant. Check the status code of the reply. See QNetworkReply::attribute().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Diplay web image

    this line : cout << (r->attribute(QNetworkRequest::HttpStatusCodeAttribut e)).toInt() << endl;
    display 200

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Diplay web image

    What about content length?

    Also verify that:
    1. the result is an image
    2. you are able to load a JPEG image from disk using the same code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Diplay web image

    What about content length?

    Also verify that:
    1. the result is an image
    2. you are able to load a JPEG image from disk using the same code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Oct 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Diplay web image

    Yes i can display a jpeg image load from disk using the same code.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Diplay web image

    What about the first issue?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Oct 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Diplay web image

    How can i do the first issue?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Diplay web image

    Save the data you get from the reply to disk and try to open it using some image viewer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Oct 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Diplay web image

    When i do this the file is empty.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Diplay web image

    In that case no wonder the image can't be created from it You have to correct your request so that it starts returning proper data.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Oct 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Diplay web image

    Finally i use QWebView, it's easier, thanks for all.

  15. #15
    Join Date
    Mar 2010
    Location
    China
    Posts
    10
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Diplay web image

    Hi protocole, I encouter the same issue as you, stole your code and made some modifies, now it works for me....

    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QtNetwork/QNetworkAccessManager>
    4. #include <QUrl>
    5. #include <QtNetwork/QNetworkRequest>
    6. #include <QtNetwork/QNetworkReply>
    7.  
    8. static QNetworkReply *reply;
    9.  
    10. class TOOTLabel : public QLabel
    11. {
    12. Q_OBJECT
    13. public:
    14. TOOTLabel(QWidget *parent =0) : QLabel(parent) {}
    15. public slots:
    16. void TOOTReadyRead()
    17. {
    18. QImage img ;
    19. img.load(reply, "JPEG");
    20. setPixmap(QPixmap::fromImage(img));
    21. show();
    22. resize(img.width(),img.height());
    23. }
    24. };
    25.  
    26.  
    27. int main(int argc, char **argv)
    28. {
    29. QApplication app(argc, argv);
    30.  
    31. QNetworkAccessManager manager;
    32. QNetworkRequest request;
    33. request.setUrl(QUrl("http://www.google.com/intl/en_ALL/images/logo2.gif"));
    34.  
    35. reply = manager.get(request);
    36. TOOTLabel imgLabel;
    37. QObject::connect( reply, SIGNAL(finished()), &imgLabel, SLOT(TOOTReadyRead()));
    38.  
    39. return app.exec();
    40. }
    41.  
    42. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Image Commander v.1.2
    By piotrek in forum Qt-based Software
    Replies: 0
    Last Post: 20th April 2009, 10:59
  2. can Qlabel display a series of image one by one ?
    By jirach_gag in forum Qt Tools
    Replies: 3
    Last Post: 11th August 2008, 15:36
  3. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10
  4. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36
  5. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 19:01

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.