PDA

View Full Version : QPixmap seems to be null but it isn't



satoshi
7th May 2009, 12:17
Hi all,
I've a strange problem setting a QPixmap on a QLabel. I call a thread which sets the QPixmap on the QLabel. That's the code I'm using:


QPixmap *pxm = new QPixmap((QString("C:/image.jpg")));
if(!pxm->isNull())
{
printf("Pixmap isn't null! %dx%d\n", this->width(), pxm->height());
this->a->w->imageLabel->setImage(this->pxm);
}
else
{
printf("Pixmap is null!\n");
}


If I put it in the run() function of the thread, it clears the image that the imageLabel was showing before (you see a gray background). If I put it in the costructor of the thread instead, it works. But in both it prints:
"Pixmap isn't null! 320x274
I set the image. 320x274 32 (183, 175, 172)"

Thats the method setImage of imageLabel:

void G2_ImageLabel::setImage(QPixmap *pxm)
{
QImage img = pxm->toImage();
QRgb px = img.pixel(10, 10);
printf("I set the image. %dx%d %d (%d, %d, %d)\n", pxm->width(), pxm->height(), pxm->depth(), qRed(px), qGreen(px), qBlue(px));
this->setPixmap(*pxm);
}

I don't know what I'm doing wrong. Please help me!

Thank you, Dario

(sorry for my english)

spirit
7th May 2009, 12:24
you can't use QPixmap in threads. use QImage insted.

satoshi
7th May 2009, 13:08
you can't use QPixmap in threads. use QImage insted.
I'm afraid, it doesn't work anyway...

spirit
7th May 2009, 13:09
whe do you use these code in a thread?


QPixmap *pxm = new QPixmap((QString("C:/image.jpg")));
if(!pxm->isNull())
{
printf("Pixmap isn't null! %dx%d\n", this->width(), pxm->height());
this->a->w->imageLabel->setImage(this->pxm);
}
else
{
printf("Pixmap is null!\n");
}

satoshi
7th May 2009, 23:06
whe do you use these code in a thread?


QPixmap *pxm = new QPixmap((QString("C:/image.jpg")));
if(!pxm->isNull())
{
printf("Pixmap isn't null! %dx%d\n", this->width(), pxm->height());
this->a->w->imageLabel->setImage(this->pxm);
}
else
{
printf("Pixmap is null!\n");
}

If I use it in the costructor of the thread I haven't problems. If I use it in the run method it doesn't work, and I need to use it there..

spirit
8th May 2009, 07:25
first of all, as I said, you can use GUI in a thread and QPixmap also.
use signals/slots or custom event for communication between threads.
read more about multithreading in Qt (http://doc.qtsoftware.com/4.5/threads.html).

wysota
8th May 2009, 08:19
If I use it in the costructor of the thread I haven't problems.

That's because the constructor of the QThread object is executed in the context of the main thread and not the worker thread that is not yet running.

satoshi
8th May 2009, 10:23
first of all, as I said, you can use GUI in a thread and QPixmap also.
use signals/slots or custom event for communication between threads.
read more about multithreading in Qt (http://doc.qtsoftware.com/4.5/threads.html).
Thank you, that helped. Problem solved :D


That's because the constructor of the QThread object is executed in the context of the main thread and not the worker thread that is not yet running.
I know, my problem was that I wasn't understanding why it wasn't working in multi-threading...

Thank you! Dario