Results 1 to 12 of 12

Thread: QImage -> QPixmap Issues all white on display

  1. #1
    Join Date
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QImage -> QPixmap Issues all white on display

    I had a program working that was getting jpg images over the network loading it into a QPixmap with the loadFromData() method and assigning the QPixmap to a QLabel with setPixmap().

    The user interface was not very responsive so I decided to speed things up a little bit I by moving the jpeg decoding over to a separate thread. I found out that QPixmap couldn't run in a secondary thread so my plan was to load the data into the QImage on the secondary thread and then on the GUI thread load the QImage into the QPixmap with fromImage().

    When I did this I ended up with a white image being displayed on the screen. To verify that things were working correctly I wrote the JPEG data to a file before loading into a QImage and then had QImage save it to another file. Both of these two files have the images I expect. I even tried moving the QImage loadFromData() back to the GUI thread just to rule out threading issues.

    I know it's got to be something I'm doing wrong but it seems so straightforward. Here's the code snippet of what I'm doing.
    Qt Code:
    1. bool foobar(uchar *p_imgbuf, int img_size)
    2. {
    3. QImage image;
    4. QPixmap my_pixmap;
    5. QLabel *p_label;
    6.  
    7. p_label = new QLabel();
    8.  
    9. status = image.loadFromData(p_imgbuf, img_size,"JPG");
    10. my_pixmap.fromImage(image);
    11. p_label->setPixmap(my_pixmap);
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 18th February 2012 at 10:09. Reason: missing [code] tags

  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: QImage -> QPixmap Issues all white on display

    What's the value of status? By the way, QPixmap has loadFromData() too, you don't have to go through QImage. Also, where do you free the buffer you pass to this method? How do you run this method?
    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
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage -> QPixmap Issues all white on display

    status returns true from both QImage loadFromData() and QPixmap fromImage().

    I was using QPixmap loadfromData() but I wanted to move that JPEG decode off of the GUI thread and QPixmap can't run in a secondary thread. The code I posted moved it back to the GUI thread just to remove any threading issues while I was tracking down this issue.

    The buffer passed to QImage keeps getting overwritten with newer images until the window is closed. The method is called whenever a new image is received over the UDP socket.

    Ian

  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: QImage -> QPixmap Issues all white on display

    So you got rid of all the threads? What if you change your uchar* buffer to QByteArray that contains the 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.


  5. #5
    Join Date
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage -> QPixmap Issues all white on display

    This is interesting I just added a save of the QPixmap and it fails. Which certainly implies that something is going wrong in moving the QImage into the QPixmap. Wish I could get more info.

    What difference would sending QByteArray make since that's going into the QImage, but my problem seems to be in converting the QImage into a QPixmap.

    Thanks

  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: QImage -> QPixmap Issues all white on display

    Quote Originally Posted by IanC View Post
    but my problem seems to be in converting the QImage into a QPixmap.
    This works just fine in thousands of applications worldwide so I guess it's not the problem. Overwriting the buffer you're using for your image might be more problematic though. Ignoring my questions will not bring you closer to a solution as well.
    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
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage -> QPixmap Issues all white on display

    Wysota I didn't mean to offend you and I certainly wasn't trying to imply that I'd found a bug. Going from a QImage to QPixmap is all over the place in examples which is why from the start I've said I must be doing something wrong.

    I also wasn't ignoring your question I was merely asking what difference do you think using a QByteArray vs the uchar* going into the QImage would make since it seems like the issue I'm having is with the conversion from a QImage to a QPixmap not with the byte buffer into the QImage.

    If overwriting off the memory was an issue I would expect the first image into the QPixmap would be good but the rest would be bad, but even the first one is just all blank and the save to a file fails.

    Anyway I tried switching to a QByteArray into the QImage and the result was the same. QImage's writing out correctly, QPixmap failing to write and all white on the screen.

  8. #8
    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: QImage -> QPixmap Issues all white on display

    I will repeat my unanswered questions then:

    • How do you run this method?
    • So you got rid of all the threads?
    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.


  9. #9
    Join Date
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage -> QPixmap Issues all white on display

    Gotha, somehow I must have missed those original questions.
    - In this code yes everything is running on the main GUI thread (for solving this problem). The code which collects up the UDP packets into a frame runs on a separate thread and sends a signal that the frame is complete with an index of the frame inside the buffer pool.
    - When the slot is received (on the main GUI thread, I verified this with the debugger, and there are no messages that the QPixmap can't be used on the non gui thread) it calls a method to get the address of the buffer and the length (yes they are protected by a mutex), it then calls the routine which i provided a snippet from. When this routine finishes the caller release the buffer so the secondary thread can reuse it.

    I just tried something else today. After I load the QImage from the buffer I call QImge.isNull() which returns false. After I convert the QImage to the QPixmap I call QPixmap.isNull() and it returns true. So something is going wrong with that conversion, even through QPixmap.fromImage() is returning true.

  10. #10
    Join Date
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage -> QPixmap Issues all white on display

    I figured out what the problem was. Somehow I didn't see that fromImage was a static member so my code was bogus
    my_pixmap.fromImage(image); should have been my_pixmap = QPixmap::fromImage(image);

  11. #11
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QImage -> QPixmap Issues all white on display

    I wonder why do you use QImage as a temporary data container?
    Wouldn't it be easier and faster to use QByteArray to store the data and load it directly to QPixmap?

  12. #12
    Join Date
    Aug 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage -> QPixmap Issues all white on display

    Because my original goal was to do the JPEG decoding on a secondary thread. QPixmap cannot run on a secondary thread but QImage can. Therefore I load (which does the decode) QImage on the secondary thread, then let QPixmap render the image on the GUI thread.

Similar Threads

  1. QPixmap/QImage serious issues between Windows/Unix
    By Largo in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2011, 14:22
  2. QPixmap replace white pixels by blacks
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 20th August 2008, 06:12
  3. DIB to QImage or QPixmap
    By ^NyAw^ in forum Qt Programming
    Replies: 0
    Last Post: 8th August 2008, 21:18
  4. issues with QImage on different computers
    By musikit in forum Installation and Deployment
    Replies: 5
    Last Post: 16th November 2007, 21:12
  5. What's faster: QPixmap-to-QImage or QImage-to-QPixmap
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2006, 18:11

Tags for this Thread

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.