Results 1 to 7 of 7

Thread: QPixmap - object disappears after fetched from http

  1. #1
    Join Date
    Jan 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QPixmap - object disappears after fetched from http

    I have a problem on a project im working on. What I want to do is fetching a image from a folder on a web server, and store them as a QPixmap object. Everything goes fine, and the image can be set inside the function which loads the data from the bytearray. But the QPixmap object as defined in the header file, won't hold the data for later use.

    Header file:
    Qt Code:
    1. #ifndef HTTPBILDE_H
    2. #define HTTPBILDE_H
    3.  
    4. #include <QBuffer>
    5. #include <QHttp>
    6. #include <QUrl>
    7. #include <QPixmap>
    8. #include <QPushButton>
    9.  
    10. class HttpBilde : public QObject {
    11. Q_OBJECT
    12.  
    13. public:
    14. HttpBilde(QString adresse);
    15. ~HttpBilde();
    16.  
    17. public slots:
    18. void finished(int requestId, bool error);
    19.  
    20. signals:
    21. void getPixmap(QPixmap);
    22.  
    23. private:
    24. QBuffer *buffer;
    25. QByteArray bytes;
    26. QHttp *http;
    27. int Request;
    28. QUrl url;
    29. QPixmap img;
    30. };
    To copy to clipboard, switch view to plain text mode 

    Cpp file:
    Qt Code:
    1. }// constructor
    2. ...
    3. url.setUrl(adresse);
    4. http = new QHttp(this);
    5. buffer = new QBuffer(&bytes);
    6.  
    7. buffer->open(QIODevice::WriteOnly);
    8. http->setHost(url.host());
    9. Request=http->get(url.path(),buffer);
    10.  
    11. connect(http, SIGNAL(requestFinished(int, bool)),this, SLOT(finished(int, bool)));
    12. }
    13.  
    14. }
    15.  
    16. void HttpBilde::finished(int requestId, bool error) {
    17. if (Request==requestId){
    18. img.loadFromData(bytes);
    19. // here I can do whatever i like with the QPixmap (img) and i get correct image data
    20. }
    21. }
    22.  
    23. QPixmap HttpBilde::getPixmap() {
    24. return img;
    25. // The returned QPixmap gets size -1,-1 no pixmap data
    26. }
    To copy to clipboard, switch view to plain text mode 

    If i send a signal from void HttpBilde::finished(int requestId, bool error) to a slot in another class, the correct pixmap data is sent. And I can set the pixmap to for example a QLabel. But i encounter the same problem in the other class if I try to store it in a QPixmap object in that class.

    I have also tried deep copy with no luck. It holds the data as long as it gets emited trough signals but disappears afterwards.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap - object disappears after fetched from http

    The first things that I see before thinking about it is that you don't have a deceleration for the getPixmap() method.
    It is bad practice to call signals and slots/methods with the same names!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap - object disappears after fetched from http

    It seems that when I put the header file on this forum I did som editing and it should be under "public" not "signals". I had rewritten the class to send out the pixmap from a signal, I will fix the original post.

    update: the option edit post was not available on the first post, but the function getPixmap should be under "public" in the header file
    Last edited by djuvsland; 26th January 2010 at 08:54.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap - object disappears after fetched from http

    But its not only the fact that it was under 'signals' - the signature is not the same!
    void getPixmap(QPixmap);

    and

    QPixmap HttpBilde::getPixmap()

    Are two different methods!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap - object disappears after fetched from http

    Yes sorry about that, but it was QPixmap getPixmap(); in the header file. What I do not understand is when I get the pixmap from a signal into a slot in another class it seems to be valid, and I can place it as an icon for a pushbutton or a label. But if I copy the data from the pixmap to a QPixmap private member object, it only holds the data as long as the slot reaches the end, and then the data disappears. I have checked even after doing a deep copy of the pixmap recieved that the data is valid and can be used, but it seems that after it stops at a function/slot end, it gets erased.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap - object disappears after fetched from http

    Just copy paste your code here, so we can help you.
    It sounds like you have a problem of a local scope variable, maybe due to naming or typo errors.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap - object disappears after fetched from http

    No need to paste the code, I just found the solution.

    This code in an external class:

    Qt Code:
    1. HttpBilde *test = new HttpBilde("http://somepictureurl");
    2. pixmap = test->getPixmap();
    3. qDebug() << pixmap.size();
    To copy to clipboard, switch view to plain text mode 

    Gets size -1,-1. Because the getPixmap() function returns the QPixmap before http has completed loading the image from the web. Got it to work with a pushbutton and connected it to a slot which ran the pixmap = test->getPixmap(); afterwards.

Similar Threads

  1. Focus in QDockWidget disappears
    By Debilski in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2011, 12:40
  2. QTableView, QSqlTableModel - data disappears
    By msh in forum Qt Programming
    Replies: 1
    Last Post: 15th November 2008, 11:50
  3. Replies: 1
    Last Post: 21st August 2008, 07:44
  4. Saving a QPixmap Object problem
    By StrikerX in forum General Programming
    Replies: 4
    Last Post: 15th November 2007, 02:19
  5. Character from argument disappears
    By Pepe in forum Qt Programming
    Replies: 13
    Last Post: 19th June 2007, 23:48

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
  •  
Qt is a trademark of The Qt Company.