When does/should "nativeSize" change for a QGraphicsVideoItem
-
I'm having some trouble getting through the initialization sequence of a program that displays the output of a webcam. In order to provide support for some additional features I'm using a
QGraphicsVideoItem
rather than the more straightforwardQCameraViewfinder
. However, I can't get video displaying correctly at first. I'll try to explain with the minimum of code... (assume the existence of the unexplained variables). I start with_videoItem = new QGraphicsVideoItem; _videoItem->setSize(QSize{w,h}); _graphicsScene = new QGraphicsScene (this); _graphicsView = new QGraphicsView (_graphicsScene); _graphicsScene->addItem(_videoItem); connect (_videoItem, &QGraphicsVideoItem::nativeSizeChanged, this, &StopMotionAnimation::frameSizeChanged);
I insert the widget into my layout and it shows a white screen in the appropriate place. Next, starting up the camera:
_camera = new QCamera(cameras.back()); connect (_camera, &QCamera::statusChanged, this, &StopMotionAnimation::cameraStatusChanged); _camera->setCaptureMode(QCamera::CaptureStillImage); _camera->setViewfinder (_videoItem); _camera->start();
Then, I have the two slots, one to monitor the camera's status and one to look for changes to the nativeSize of the videoItem, which starts out at (-1,-1). The camera slot triggers just fine, executing as expected:
void StopMotionAnimation::cameraStatusChanged(QCamera::Status status) { switch(status) { case QCamera::UnavailableStatus: [[fallthrough]]; case QCamera::UnloadedStatus: [[fallthrough]]; case QCamera::LoadingStatus: [[fallthrough]]; case QCamera::UnloadingStatus: [[fallthrough]]; case QCamera::LoadedStatus: [[fallthrough]]; case QCamera::StandbyStatus: [[fallthrough]]; case QCamera::StartingStatus: [[fallthrough]]; case QCamera::StoppingStatus: qDebug() << "Camera status changed to " << status; break; case QCamera::ActiveStatus: _videoItem->show(); _graphicsView->show(); qDebug() << "QGraphicsVideoItem"; qDebug() << " nativeSize: " << _videoItem->nativeSize(); break; } }
The debug statement there prints out (-1,-1) when it executes the first time. However, if I basically just call the same sequence of functions a second time, the nativeSize return (640,480) and the video shows on my screen. The function connected to nativeSizeChanged never fires in any circumstance. Clearly I am doing something out of order here, but I can't figure out what it is. Any suggestions?
-
Hi,
Why show the video item before the graphics view ? AFAIK, only the second should be needed.
-
I'm throwing darts at the wall to see what sticks -- since the video is not showing correctly I've tried all sorts of combinations of showing the various individual components. Right now, the only thing that works is if I create a
QCameraImageCapture
, attach it to the camera, and after a couple of seconds callsetEncodingSettings
on it, that seems to trigger the videoItem to show the image. Interestingly, even if no image is showing, that imageCapture object correctly pulls the data off of the webcam and stores it to a file.