Results 1 to 4 of 4

Thread: Showing QImage without converting to QPixmap

  1. #1
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Showing QImage without converting to QPixmap

    I have some data:

    Qt Code:
    1. uchar* imageData;
    To copy to clipboard, switch view to plain text mode 

    then;

    Qt Code:
    1. imageData = static_cast <uchar*>(realloc(imageData, imageSize));
    2. renderer->render(imageData, imageWidth, imageHeight, imageSize);
    To copy to clipboard, switch view to plain text mode 

    and showing image up:
    Qt Code:
    1. QImage image(imageData, imageWidth, imageHeight, imageBytesPerLine, QImage::Format_RGB888); //I need only RGB
    2. QPixmap pixmap = QPixmap::fromImage(image);
    3. ui->imagePlace->setPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 

    where
    imagePlace is QLabel

    Everything is all right until I want create image which takes lot of memory, for example size 8000px x 12000px
    then allocated memory for imageData takes about 366MB (only 366MB).
    Image still appears, but when doing
    Qt Code:
    1. QPixmap pixmap = QPixmap::fromImage(image);
    2. ui->imagePlace->setPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 
    Memory usage jump to above 700MB instead of 366MB

    So my question is how to prevent this situation, because I want to show image which takes about 1GB or more, but in that situation memory usage is doubled (as far as I can see)!!

    Is there way to direct display QImage on the screen?
    What's more I wanna refresh this image several times (some kind of software generated animation)

    I had search for reasonable solution but without success as far.

    another strange thing:

    when trying to display (in QLabel) image which has mode than 32767px width or height it fails, nothing is displayed.
    It could be 32767px x 10px (takes only 959,97 KiB) and it shows correctly, but 32768px x 10px fails to show ...
    Last edited by travick; 3rd December 2011 at 20:01.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Showing QImage without converting to QPixmap

    Create a subclass of QLabel, reimplement paintEvent and draw the QImage with QPainter::drawImage.

  3. #3
    Join Date
    Dec 2011
    Posts
    21
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Showing QImage without converting to QPixmap

    I subclassed QLabel

    Qt Code:
    1. void ImagePlacer::paintEvent (QPaintEvent *event){
    2. if (image != 0)
    3. {
    4. this->resize(image->size());
    5. QPainter painter(this);
    6. painter.drawImage(this->rect(), *image, this->rect());
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    but there is the same dobled memory usage ... It seem that problem is somewhere else, maybe in QPainter?

    --
    edit:
    QPainter draws image by copying it's part, so there is the problem ....

    solution - not resizing QLabel
    Qt Code:
    1. void ImagePlacer::paintEvent (QPaintEvent *event){
    2. if (image != 0)
    3. {
    4. QPainter painter(this);
    5. painter.drawImage(this->rect(), *image, this->rect());
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    but I've to show scroll bars, to move over whole image ...
    Last edited by travick; 3rd December 2011 at 21:21.

  4. #4
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Showing QImage without converting to QPixmap

    Hi,

    You can resize your QLabel-derived class at some other point (when setting the image for example) instead of resizing it each time in the paintEvent() handler.
    Qt Code:
    1. void ImagePlacer::setImage (QImage * newImage)
    2. {
    3. image = newImage;
    4. if (0 != image)
    5. resize (image->size());
    6. }
    To copy to clipboard, switch view to plain text mode 
    Added bonus: it will resize the widget only once instead of each time it is repainted.

Similar Threads

  1. Converting from HBitmap to a QPixmap
    By qtUser500 in forum Newbie
    Replies: 6
    Last Post: 3rd March 2009, 17:26
  2. DIB to QImage or QPixmap
    By ^NyAw^ in forum Qt Programming
    Replies: 0
    Last Post: 8th August 2008, 20:18
  3. What's faster: QPixmap-to-QImage or QImage-to-QPixmap
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2006, 17:11
  4. QImage or QPixmap?
    By Dark_Tower in forum Qt Programming
    Replies: 9
    Last Post: 1st April 2006, 17:08
  5. QPixmap designer property not showing up
    By high_flyer in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2006, 19:56

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.