Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The program has unexpectedly finished

The program has unexpectedly finished

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WhatIf
    wrote on last edited by WhatIf
    #1

    Hi,

    I'm trying to have my webcam capture still images. I'm building the code step by step. So far, I have the code to check if a camera is available and display an error if no cams available. The code complies and run but as soon as I click the open cam button on mainWindow, the program stops working. Here is the code for the camera dialog:

    camera.h

    #ifndef CAMERA_H
    #define CAMERA_H
    
    #include <QDialog>
    
    #include <QCamera>
    #include <QCameraImageCapture>
    
    namespace Ui {
    class Camera;
    }
    
    class Camera : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Camera(QWidget *parent = 0);
        ~Camera();
    
    private slots:
        void on_cancelBtn_clicked();
    
    private:
        Ui::Camera *ui;
    
        QCamera *camera;
        QCameraImageCapture *imageCapture;
    };
    
    #endif // CAMERA_H
    
    

    Camera.cpp

    #include "camera.h"
    #include "ui_camera.h"
    
    #include <QCameraInfo>
    #include <QMessageBox>
    
    Camera::Camera(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Camera)
    {
        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("No Camera is available Error"), camera->errorString());
        }
    }
    
    Camera::~Camera()
    {
        delete ui;
        delete imageCapture;
        delete camera;
    }
    
    void Camera::on_cancelBtn_clicked()
    {
        close();
    }
    

    What am I doing wrong?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      If "open cam" crashes the app it would be nice to see the code of it. Otherwise it's guessing.

      Other than that - you're not zero-initializing your pointer variables camera and imageCapture. They have a garbage value.
      If there are no cameras the camera pointer is uninitialized and so calling camera->errorString() is a bug.

      W 1 Reply Last reply
      0
      • Chris KawaC Chris Kawa

        If "open cam" crashes the app it would be nice to see the code of it. Otherwise it's guessing.

        Other than that - you're not zero-initializing your pointer variables camera and imageCapture. They have a garbage value.
        If there are no cameras the camera pointer is uninitialized and so calling camera->errorString() is a bug.

        W Offline
        W Offline
        WhatIf
        wrote on last edited by
        #3

        @Chris-Kawa

        Here is the code inside "open cam"

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        #include "camera.h"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::on_pushButton_clicked()
        {
            Camera cam;
            cam.setModal(true);
            cam.setWindowFlags(Qt::CustomizeWindowHint); //Set window with no title bar
            cam.setWindowFlags(Qt::FramelessWindowHint); //Set a frameless window
            cam.exec();
        }
        

        I disconnected the cam to find out if it will be able to detect that there are no cameras. How can I get around the camera pointer is uninitialized?

        W 1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          How can I get around the camera pointer is uninitialized?

          Well, by initializing it ;)

          Camera::Camera(QWidget *parent)
             : QDialog(parent), ui(new Ui::Camera), camera(nullptr), imageCapture(nullptr)
          {
              ...
          

          and not calling it when it's not initialized:

          {
             QMessageBox::warning(this, tr("Error"), tr("No Camera is available."));
          }
          
          1 Reply Last reply
          0
          • W WhatIf

            @Chris-Kawa

            Here is the code inside "open cam"

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            
            #include "camera.h"
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            void MainWindow::on_pushButton_clicked()
            {
                Camera cam;
                cam.setModal(true);
                cam.setWindowFlags(Qt::CustomizeWindowHint); //Set window with no title bar
                cam.setWindowFlags(Qt::FramelessWindowHint); //Set a frameless window
                cam.exec();
            }
            

            I disconnected the cam to find out if it will be able to detect that there are no cameras. How can I get around the camera pointer is uninitialized?

            W Offline
            W Offline
            Wilk
            wrote on last edited by
            #5

            @WhatIf Hi
            Try to create a pointer to Camera object instead of object itself in you slot. I guess that you Camera object may just be destroyed when it gets out of scope.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Wilk The Camera object is a dialog and a blocking exec() is called on it. It only goes out of scope when it's already closed and that is the preferred way to use dialogs. The crash is most likely due to the usage of uninitialized variables.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved