PDA

View Full Version : Showing QImage without converting to QPixmap



travick
3rd December 2011, 19:46
I have some data:


uchar* imageData;

then;



imageData = static_cast <uchar*>(realloc(imageData, imageSize));
renderer->render(imageData, imageWidth, imageHeight, imageSize);


and showing image up:


QImage image(imageData, imageWidth, imageHeight, imageBytesPerLine, QImage::Format_RGB888); //I need only RGB
QPixmap pixmap = QPixmap::fromImage(image);
ui->imagePlace->setPixmap(pixmap);


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


QPixmap pixmap = QPixmap::fromImage(image);
ui->imagePlace->setPixmap(pixmap);

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 ...

stampede
3rd December 2011, 20:33
Create a subclass of QLabel, reimplement paintEvent and draw the QImage with QPainter::drawImage (http://doc.qt.nokia.com/latest/qpainter.html#drawImage-7).

travick
3rd December 2011, 21:12
I subclassed QLabel


void ImagePlacer::paintEvent (QPaintEvent *event){
if (image != 0)
{
this->resize(image->size());
QPainter painter(this);
painter.drawImage(this->rect(), *image, this->rect());
}
}

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

void ImagePlacer::paintEvent (QPaintEvent *event){
if (image != 0)
{
QPainter painter(this);
painter.drawImage(this->rect(), *image, this->rect());
}
}


but I've to show scroll bars, to move over whole image ...

jlemaitre
13th January 2012, 20:54
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.


void ImagePlacer::setImage (QImage * newImage)
{
image = newImage;
if (0 != image)
resize (image->size());
}

Added bonus: it will resize the widget only once instead of each time it is repainted.