PDA

View Full Version : Change QPixmap image at runtime



Qt Coder
28th March 2009, 08:19
Hello all,
I m using Qt 4.3.4


My program has one QLabel in which I m dispalying an image using QPixmap.

I want to load another image in QPixmap at runtime..

How to do that???

Lykurg
28th March 2009, 08:46
I want to load another image in QPixmap at runtime...
In the QPixmap or in the QLabel?

mylabel->setPixmap(QPixmap("newFile"));
// or
mypixmap->load("newFile");
mylabel->setPixmap(mypixmap);?

Qt Coder
28th March 2009, 09:00
QPixmap PixEllipseNose;
PixEllipseNose.load(QString::fromUtf8("images/Ellipse1.PNG"));
ui.lblNoseEllipse->setPixmap(PixEllipseNose);


Later some time in program (after timer goes off )I need to display another image

I want to load another image in existing PixEllipseNose but I dont want to set another pixmap in Qlabel .

How to do it???

Lykurg
28th March 2009, 09:03
You must create the QPixmap on the heap to do so. "QPixmap PixEllipseNose;" is deleted after the scope. Use
QPixmap *m_PixEllipseNose = new QPixmap(...);

Qt Coder
28th March 2009, 09:45
Hey but I declared "QPixmap PixEllipseNose;" as public variable in my class .

I m loading image in it in Class constructor.

Also the function in which I want to load another image in " PixEllipseNose" is a member function declared in "public" section.



//Class constructor

GLT_Submenu::GLT_Submenu(QWidget * parent):QDialog(parent)
{
ui.setupUi(this);
PixEllipseNose.load(QString::fromUtf8("images/Ellipse1.PNG"));
ui.lblNoseEllipse->setPixmap(PixEllipseNose);
}


void GLT_Submenu::f_FillNoseEllipse(int m_Stage)
{
QPen GreenPen(Qt::green, 1);
QPainter p(&PixEllipseNose);
p.setPen(GreenPen);
p.drawLine(EllipseNoseX1,EllipseNoseY1,EllipseNose X2,EllipseNoseY2);
ui.lblNoseEllipse->setPixmap(PixEllipseNose);
if(m_Stage ==LOAD_YELLOW_PEN)
{
//add Code to display next image in PixEllipseNose
}

}

Lykurg
28th March 2009, 09:50
Hey but I declared "QPixmap PixEllipseNose;" as public variable in my class .
Yeah, forget about the heap thing.

And where is now your problem. Sorry can't get the point. Just use load() or set it to an QPainter to use it again. Everywhere you want.

EDIT: If you want to use it with a painter you may want to clear the pixmap first.

Qt Coder
28th March 2009, 09:53
Hey I tried using


PixEllipseNose.load(QString::fromUtf8("Images/YellowEllipse.PNG"));

but the image is not changed ,still displayes the old image...

Now what to do?

Lykurg
28th March 2009, 09:59
You have to set it on your label once again, because QLabel::setPixmap() copies your pixmap. So PixEllipseNose isn't a pointer to QLabel::pixmap().

Qt Coder
28th March 2009, 10:10
I m using code



PixEllipseNose.load(QString::fromUtf8("Images/YellowEllipse.PNG"));
ui.lblNoseEllipse->setPixmap(PixEllipseNose);


But it doesnt change image.


if I use



ui.lblNoseEllipse->setPixmap(QPixmap(QString::fromUtf8("Images/YellowEllipse.PNG")));

it displayes the next image.

But I want to change contents of "PixEllipseNose" instead off making QLabel to point to another pixmap.....

aamer4yu
28th March 2009, 10:16
But I want to change contents of "PixEllipseNose" instead off making QLabel to point to another pixmap.....
The contents of the pixmap will be changed,,, but how do you plan to see that the contents have been changed ? you need to paint it somewhere...dont u ?
and using QLabel::setPixmap is one of the easiest way, without making your own classes, and doing the painting yourself.

Lykurg
28th March 2009, 10:19
void QLabel::setPixmap(const QPixmap &pixmap)
{
Q_D(QLabel);
if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) {
d->clearContents();
d->pixmap = new QPixmap(pixmap); //<- it copies your Pixmap
}

if (d->pixmap->depth() == 1 && !d->pixmap->mask())
d->pixmap->setMask(*((QBitmap *)d->pixmap));

d->updateLabel();
}

So your try to avoid increase memory are obsolete.

EDIT: Why your first option is not working has to do with the cacheKey. But that I also don't understand right.

Qt Coder
30th March 2009, 12:09
Hey the thing is

I m painting this QPixmap image (Drawing some lines over the image using QPainter)

Now after some period of time I need the original image in QPixmap and start my drawing again.

I just want to change image of my Qpixmap at runtime .


How to aceive this??

Qt Coder
30th March 2009, 13:37
Hey I got the solution to above problem



QPixmap PixEllipseNoseGreen,PixEllipseNoseGreenBKP;
PixEllipseNoseGreen.load(QString::fromUtf8("images/GreenEllipse.PNG"));
PixEllipseNoseGreenBKP.load(QString::fromUtf8("images/GreenEllipse.PNG"));



After I finished painting QPixmap,I load the backup image as followes


PixEllipseNoseGreen.operator=(PixEllipseNoseGreenB KP);