How to make QCamera and QGraphicsVideoItem?

There is code:
#include <qtmultimediawidgets>

int main(int argc, char *argv[])
{
 QApplication * app = new QApplication(argc,argv);

 QGraphicsView * view = new QGraphicsView();
 QGraphicsScene * scene = new QGraphicsScene();
 QGraphicsVideoItem * item = new QGraphicsVideoItem();
view->setScene(scene);
scene->addItem(item);
view->show();

 QCamera * camera;
 if(QCamera::availableDevices().count() > 0)
{
 camera = new QCamera();
camera->setViewfinder(item);
camera->start();
}

 return app->exec();
}
</qtmultimediawidgets>

Well it compiles and runs, the window appears, but during runtime the camera swears:
no video surface, cant start
Camera state changed: QCamera::LoadedState

What could be wrong? Qt 5.1.1, Windows 7 x64
October 3rd 19 at 03:26
1 answer
October 3rd 19 at 03:28
Solution
I have the same thing. If you are urgent, that is not correct, you can do this:

#include <qtgui qtgui>
#include <qtwidgets qtwidgets>
#include <qtmultimediawidgets qtmultimediawidgets>

int main(int argc, char *argv[]) {
 QApplication *app = new QApplication(argc,argv);

 QGraphicsView *view = new QGraphicsView();
 QGraphicsScene *scene = new QGraphicsScene();
 QVideoWidget *widget = new QVideoWidget();
view->setScene(scene);
scene->addWidget(widget);
view->show();

 QCamera *camera;
 if(QCamera::availableDevices().count() > 0) {
 camera = new QCamera();
camera->setViewfinder(widget);
camera->start();
}

 return app->exec();
}
</qtmultimediawidgets></qtwidgets></qtgui>
Thanks, widget, Yes, it works (actually I did after posting the question). Just for the first time confronted with the fact that officially documented function in practice has not worked. Oh, the magical world of open-source. But Qt is worth it — IMHO the best cross-platform C++-based development platform.

By the way, maybe you know, maybe there is easier than here like this, with the overriding abstraktnogo class and virtual function, a way to render with the camera image, in this case using QVideoWidget, mirrored horizontally? - buster_Keebler10 commented on October 3rd 19 at 03:31
 QGraphicsProxyWidget *proxyWidget = scene->addWidget(widget);
 QTransform transform;
 transform.scale(-1, 1);
proxyWidget->setTransform(transform);
- kurtis commented on October 3rd 19 at 03:34
Thank you, the mirror works! For the beginner, 1.5 Qt weeks ago for the first time in eyes saw, thought of it, I think, is absolutely unreal. - buster_Keebler10 commented on October 3rd 19 at 03:37

Find more questions by tags Qt