PDA

View Full Version : QImage and drawImage method



ally
3rd July 2015, 07:36
uso il thread per chiedere un informazione simile : ho un QFrame su cui ho necessità di visualizzare e nascondere 10 immagini fisse. E' corretto caricarle in un metodo di init tramite un metodo simile :

Hi, I have a question about best way to use painter.drawImage : I need to show and hide 10 images in a QFrame. I decided to init them using pointer QImage in a init() method, then when needed show them by paint call. Is it a good way or maybe can be memory problems?

void MiaClasse::init(){

image1 = new QImage;
image1->load("resources/images/image1.png");

}

void MiaClasse::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawImage(155, 130, *image1);
}

thank you

yeye_olive
3rd July 2015, 10:00
Your solution works--for displaying one image anyway--, but you could make a few improvements.

Firstly, your code leaks memory because the QImage is never deallocated. You could replace image1 with a smart pointer (such as std::unique_ptr), or even not go through a pointer at all: let image1 be a QImage. It will be allocated/deallocated with the containing MiaClasse instance.

Secondly, you had the right intuition by preloading the image instead of loading it from disk in paintEvent(), but you could do even better by using QPixmap instead of QImage. The former is more readily usable by a painter.

ally
3rd July 2015, 14:07
thank you, but init method is called one time only when application start so memory for any image is allocated only one time. Isn't it?
I also need to make a couple of them blinking so I prefer to load images only one time and paint when needed :



void MiaClasse::init(){

image1 = new QImage;
image1->load("resources/images/image1.png");

image2 = new QImage;
image2->load("resources/images/image2.png");

blink = false;

}

void MiaClasse::letItBlink(){

blink = !blink;
repaint();

}

void MiaClasse:: paintEvent(QPaintEvent *)
{
QPainter painter(this);

painter.drawImage(155, 130, *image1);

if(blink)
painter.drawImage(155, 130, *image2);
}

yeye_olive
6th July 2015, 10:56
You current design works; loading the images once, upon initialization, instead of every time the widget is painted is the right way. I was not suggesting changing that, quite the contrary.

Instead of allocating QImages on the heap, you could allocate QPixmaps directly with the MiaClasse:



class MiaClasse /* ... */ {
/* ... */
private:
QPixmap image1;
QPixmap image2;
}

void MiaClasse::init(){
image1.load("resources/images/image1.png");
image2.load("resources/images/image2.png");
blink = false;
}

void MiaClasse::letItBlink(){
blink = !blink;
update(); // Call this instead of QWidget::repaint()
}

void MiaClasse:: paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.drawPixmap(155, 130, image1);
if(blink)
painter.drawPixmap(155, 130, image2);
}