Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How do you add a Camera class to a .ui file correctly?



  • I have a general ui page (video_display.ui) (Qt designer) with a layout and several components. I have set a blank widget named video within that page and promote it to class QCameraViewfinder. If I run the following code from within the cpp (video_display.cpp) file, I get the result I want.

    Video_display::Video_display(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Video_display)
    {
        ui->setupUi(this);
    ui->set
     mCamera = new QCamera(this);
     mCameraViewfinder = ui->video;
     mCamera->setViewfinder(mCameraViewfinder);
     ui->video->show();
     mCamera->start();
    }
    

    However, I want to implement everything related to the camera in a separate class.
    So instead of promoting to QCameraViewfinder, I promote it to Camera which is a custom class.

    Camera::Camera(QWidget *parent) : QWidget(parent)
    {
    
         mCamera = new QCamera();
         mCameraViewfinder = new QCameraViewfinder(this);
         mCamera->setViewfinder(mCameraViewfinder);
         mCamera->start();
    }
    

    The problem with this is that while the camera is in the correct location it is not scaled to fit within the video widget thus only displaying a portion of the video screen.

    If I add the following line of code:

    mCamerViewfinder->show();
    

    The video is only displayed in a small frame within the video widget location.

    Any guidance on the matter would be very helpful.
    Thank you,
    Arjun



  • I added the following two lines of code and it worked:

         QHBoxLayout *camera_layout = new QHBoxLayout(this);
         mCamera = new QCamera();
         mCameraViewfinder = new QCameraViewfinder();
         camera_layout->addWidget(mCameraViewfinder);
         mCamera->setViewfinder(mCameraViewfinder);
         mCamera->start();
    

    And it worked