I'd really like to attach to my web camera, process the frames I get out of it, and then display the processed frame in a window with some super-imposed markings.

The strategy I've been trying is to have a QGraphicsView with a QGraphicsScene that has a QGraphicsPixmap added. I then use a custom VideoSurface to grab frames. Right now I'm trying to just transfer the frames to the QGraphicsView (no extra markings or processing) but if I call setPixmap the program will crash just after the frame handler returns. If I skip the call to setPixmap, it doesn't crash. Could anyone point me in the right direction?

Qt Code:
  1. MainWindow::MainWindow(QWidget *parent) :
  2. QMainWindow(parent),
  3. ui(new Ui::MainWindow),
  4. frame(640, 480)
  5. {
  6. ui->setupUi(this);
  7. QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
  8. cam = new QCamera(cameras.first(), this);
  9. cam->load();
  10. frame_grabber = new CameraFrameGrabber();
  11. cam->setViewfinder(frame_grabber);
  12. cam->setCaptureMode(QCamera::CaptureViewfinder);
  13. connect(frame_grabber, SIGNAL(frameAvailable(QImage)), this, SLOT(handleFrame(QImage)));
  14. cam->start();
  15. frame_timer.start();
  16. scene = new QGraphicsScene();
  17. pix = new QGraphicsPixmapItem(frame);
  18. scene->addItem(pix);
  19. ui->gv_viewfinder->setScene(scene);
  20. }
  21.  
  22. MainWindow::~MainWindow()
  23. {
  24. delete ui;
  25. }
  26.  
  27. void MainWindow::handleFrame(const QImage& img)
  28. {
  29. frame_times.push_back(frame_timer.elapsed());
  30. frame_timer.restart();
  31. if (frame_times.size() > 10) frame_times.pop_front();
  32. int frame_count = frame_times.size();
  33. int frame_total = 0;
  34. for(int i = 0; i < frame_count; ++i)
  35. {
  36. frame_total += frame_times[i];
  37. }
  38. double fps = 0;
  39. if (frame_count)
  40. {
  41. fps = (frame_total / frame_count);
  42. }
  43. ui->sb_fps->setValue(fps);
  44.  
  45. bool good_pix = frame.convertFromImage(img);
  46. if (good_pix) pix->setPixmap(frame);
  47. this->update();
  48. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. bool CameraFrameGrabber::present(const QVideoFrame &frame)
  2. {
  3. if (frame.isValid()) {
  4. QVideoFrame cloneFrame(frame);
  5. cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
  6. const QImage image(cloneFrame.bits(),
  7. cloneFrame.width(),
  8. cloneFrame.height(),
  9. QVideoFrame::imageFormatFromPixelFormat(cloneFrame .pixelFormat()));
  10. emit frameAvailable(image);
  11. cloneFrame.unmap();
  12. return true;
  13. }
  14. return false;
  15. }
To copy to clipboard, switch view to plain text mode