Add QImage to QLabel
-
Hey guys, I have the following problem. I receive a QVideoFrame from my camera. I convert this to a Qimage and save it as a QPixmap in a label. However, this does not display the image. But when I load the pixmap from the label and save it locally, I can see the image. If I save the image locally before writing it to the label, reload it, and then insert the loaded image into the label, it works.
Class where I get the QVideoFrame. This function is always called when a new camera image is available.
connect( m_io->getCamera( ), &Camera::imageAvailable, this, &Measurement::newImageAvailable );
void Measurement::newImageAvailable( ) { static QDateTime dt = QDateTime::currentDateTime( ); if ( dt.msecsTo( QDateTime::currentDateTime( ) ) < 100 ) { return; } auto frame = m_io->getCamera( )->getVideoFrame( ); dt = QDateTime::currentDateTime( ); QImage img = frame.toImage( ); camDialog.updateLable( QPixmap::fromImage( frame.toImage( ) ) ); }
void CameraDialog::updateLable( QPixmap pixMap ) { ui->label->setPixmap(pixMap); }
The size of the label should also fit.
I hope anybody can help. -
Hey guys, I have the following problem. I receive a QVideoFrame from my camera. I convert this to a Qimage and save it as a QPixmap in a label. However, this does not display the image. But when I load the pixmap from the label and save it locally, I can see the image. If I save the image locally before writing it to the label, reload it, and then insert the loaded image into the label, it works.
Class where I get the QVideoFrame. This function is always called when a new camera image is available.
connect( m_io->getCamera( ), &Camera::imageAvailable, this, &Measurement::newImageAvailable );
void Measurement::newImageAvailable( ) { static QDateTime dt = QDateTime::currentDateTime( ); if ( dt.msecsTo( QDateTime::currentDateTime( ) ) < 100 ) { return; } auto frame = m_io->getCamera( )->getVideoFrame( ); dt = QDateTime::currentDateTime( ); QImage img = frame.toImage( ); camDialog.updateLable( QPixmap::fromImage( frame.toImage( ) ) ); }
void CameraDialog::updateLable( QPixmap pixMap ) { ui->label->setPixmap(pixMap); }
The size of the label should also fit.
I hope anybody can help.@itzwich1 said in Add QImage to QLabel:
camDialog.updateLable
Does your measurement class really have the dialog to show the image as class member? This is bad design as they should not know each other. Pass the image through a signal/slot connection.
-
You're right. But I have to call up the dialog within my measurement class. Even if I change that, it doesn't solve my original problem.
@itzwich1 said in Add QImage to QLabel:
Even if I change that, it doesn't solve my original problem.
How? Please provide a minimal compileable example of your problem. I would guess you have two instances of your dialog.
-
You can print some information. Have you confirmed that everytime
frame
has data andimg
is valid?
I guess maybe some null images are set after some valid ones.@Bonnie said in Add QImage to QLabel:
You can print some information. Have you confirmed that everytime
frame
has data andimg
is valid?
I guess maybe some null images are set after some valid ones.The image should be valid. frame.isValid always returns true and QImage.isNull always returns false. Why would it be bad if an image were empty? It shouldn't matter, right?
-
@Bonnie said in Add QImage to QLabel:
You can print some information. Have you confirmed that everytime
frame
has data andimg
is valid?
I guess maybe some null images are set after some valid ones.The image should be valid. frame.isValid always returns true and QImage.isNull always returns false. Why would it be bad if an image were empty? It shouldn't matter, right?
@itzwich1
If a frame image were empty it would be fine but you wouldn't see anything, which might be what you are reporting.I don't think anyone can guess from what you say. If it's not that, for all we know you might have a different dialog or a different label from what you think you are working with. There is so much you can try to help you debug. Make sure you understand how many times your slot is being called. Save the image to file from
newImageAvailable( )
not from the label. Debug out information about the image. Load a saved, good image innewImageAvailable( )
instead of reading it from video and send that toupdateLable()
. Are you re-entering the Qt event loop after you have changed the label, else you won't see it?I know nothing about this area. But I don't see any Qt method
getVideoFrame( )
. I don't know whether you ought be looking at QMediaCaptureSession Class for this kind of capture.