QPixmap seems to be null but it isn't
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:
Code:
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:
Code:
void G2_ImageLabel
::setImage(QPixmap *pxm
) {
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)
Re: QPixmap seems to be null but it isn't
you can't use QPixmap in threads. use QImage insted.
Re: QPixmap seems to be null but it isn't
Quote:
Originally Posted by
spirit
you can't use QPixmap in threads. use QImage insted.
I'm afraid, it doesn't work anyway...
Re: QPixmap seems to be null but it isn't
whe do you use these code in a thread?
Code:
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");
}
Re: QPixmap seems to be null but it isn't
Quote:
Originally Posted by
spirit
whe do you use these code in a thread?
Code:
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..
Re: QPixmap seems to be null but it isn't
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.
Re: QPixmap seems to be null but it isn't
Quote:
Originally Posted by
satoshi
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.
Re: QPixmap seems to be null but it isn't
Quote:
Originally Posted by
spirit
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.
Thank you, that helped. Problem solved :D
Quote:
Originally Posted by
wysota
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