Getting camera's viewfinder to work



  • Hi again :)

    I'm in the step before last to getting the camera to capture images. Here is the code so far

    #include "camera.h"
    #include "ui_camera.h"
    
    #include <QCameraInfo>
    #include <QMessageBox>
    #include <QCameraViewfinder>
    
    
    Camera::Camera(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Camera),
        camera(0),
        imageCapture(0)
    {
        ui->setupUi(this);
    
        //Camera code begins here
    
        if (QCameraInfo::availableCameras().count() > 0)
        {
            QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
            foreach (const QCameraInfo &cameraInfo, cameras) {
                if (cameraInfo == QCameraInfo::defaultCamera())
                    camera = new QCamera(cameraInfo);
                }
        }
        else
        {
            QMessageBox::warning(this, tr("Error"), tr("No Camera is available."));
            QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);
        }
    }
    
    Camera::~Camera()
    {
        delete ui;
        delete imageCapture;
        delete camera;
    }
    
    void Camera::on_cancelBtn_clicked()
    {
        close();
    }
    
    void Camera::on_startCameraBtn_clicked()
    {
        QCameraViewfinder *viewfinder = new QCameraViewfinder;
        camera->setViewfinder(ui->graphicsView->viewfinder);
        viewfinder->show();
        camera->start();
    }
    

    I keep getting this error:

    C:\Users......\campopup\camera.cpp:50: error: 'class QGraphicsView' has no member named 'viewfinder'
    camera->setViewfinder(ui->graphicsView->viewfinder);

    Basically the app is a mainWindow with a button "open cam" once clicked it, if a cam is available, a dialog opens up which has a QGraphicsView and four buttons (start camera, capture, save, and cancel). The view from the cam should be displayed inside of QGraphicsView.


  • Qt Champions 2016

    Hello,

    ui->graphicsView->viewfinder
    

    Well, to cite the compiler: "class QGraphicsView has no member named 'viewfinder'". You probably meant:

    camera->setViewfinder(viewfinder);
    

    Additionally, this: QCameraViewfinder *viewfinder = new QCameraViewfinder; is a memory leak. Qt is not Java, you're supposed to clean up your objects one way or another (usually by giving them a parent).

    Kind regards.



  • @kshegunov

    But how would the program know to display the camera view in QGraphicsView?

    I'm moving from Java to C++ and it's not as easy as I thought. If I add "QCameraViewfinder *viewfinder;" to camera.h private section and add a "delete viewfinder;" in ~Camera(), will that solve the memory leak?


  • Qt Champions 2016

    Hello,
    Right, I didn't event check what you're trying to add to the graphics view. You can't put QCamera in it in any case! In the graphics view you put QGraphicsItems through the scene - QGraphicsScene. QCamera doesn't derive from QGraphicsItem so you can't use it like this. Why would you even want to put in a graphics view?

    About the memory leak, just set a parent to the viewfinder when creating it, like this:

    QCameraViewfinder *viewfinder = new QCameraViewfinder(this);
    

    and you'll be fine. Qt will clean up the object when the Camera class' instance is destroyed.

    Kind regards.



  • @kshegunov

    This is one of the parts that's confusing me. Is QCameraViewfinder a widget in which live streaming from the webcam is displayed until I chose to take a still picture? Or do I need to create a widget in which in which the live streaming from the webcam is displayed? This is why I added the graphics view using qt designer so that the video/live streaming from the cam is displayed?

    I'm kind of new to Qt and use the designer to create almost all gui but starting to learn as much coding because from time to time designer alone won't do. In the Widget Box, I searched for QCameraViewfinder and QGraphicsScene, nothing came out.

    Your clarification will be much appreciated :)


  • Qt Champions 2016

    @WhatIf said:

    This is one of the parts that's confusing me. Is QCameraViewfinder a widget in which live streaming from the webcam is displayed until I chose to take a still picture?

    Yes, as far as I understand it. See this example, is it what you're after?

    I'm kind of new to Qt and use the designer to create almost all gui but starting to learn as much coding because from time to time designer alone won't do.

    You could promote a generic widget to whatever you wish. You put a QWidget in the designer, and on the right side where objects are listed, you click with the right button on it and there's "promote" in the context menu. Fill up the data in the dialog box that appears and you have a viewfinder widget.

    Kind regards.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.