Unsolved QCameraViewFinder freezes and QCameraImageCapture stops saving
-
Hi!
I have a QCameraViewFinder and a QCameraImageCapture. Somewhat randomly the viewfinder just freezes and stops showing a moving image. When this happens QCameraImageCapture also stops saving images to disk.
Strangely, QCamera also does not seem to fire any signals as my connected slots do nothing. Maybe I made an error? Here is my code (I'm on Qt 5.8.0 and OSX Sierra). The main code is in
void MainWindow::on_pushButton_clicked()Thanks in advance!
/JC
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); camera = new QCamera(this); viewfinder = new QCameraViewfinder(this); camera->setViewfinder(viewfinder); imageCapture = new QCameraImageCapture(camera); connect(camera, SIGNAL(locked()), this, SLOT(my_camera_lock())); connect(camera, SIGNAL(lockFailed()), this, SLOT(my_lock_failed())); ui->verticalLayout->addWidget(viewfinder); viewfinder->show(); camera->start(); } void MainWindow::on_pushButton_clicked() { for(int i = 0; i < 40; i++){ QString path = "/Users/JCRohner/Junk/image" + QString::number(i) + ".jpg"; QApplication::beep(); camera->searchAndLock(); while(!imageCapture->isReadyForCapture()){ QApplication::processEvents(QEventLoop::AllEvents, 100); } imageCapture->capture(path); camera->unlock(); delayForSecs(3); } } void MainWindow::my_camera_lock(){ qDebug()<<"locked"; } void MainWindow::my_lock_failed(){ qDebug()<<"lock failed"; } void MainWindow::delayForSecs(int secs){ QTime dieTime = QTime::currentTime().addSecs(secs); while (QTime::currentTime() < dieTime){ QCoreApplication::processEvents(QEventLoop::AllEvents, 100); } } MainWindow::~MainWindow() { delete ui; }
-
@JRohner Did you verify that connect() succeeded?
qDebug() << connect(camera, SIGNAL(locked()), this, SLOT(my_camera_lock())); qDebug() << connect(camera, SIGNAL(lockFailed()), this, SLOT(my_lock_failed()));
Also the way your doing it is not really nice: you implement a blocking slot and use QApplication::processEvents to not to block the event loop. You should consider using QTimer.
-
Thanks for your suggestions @jsulm ! I made the changes you proposed (see code below). The connections succeed as i get "true" and "true" using your code. My slots still do not get called however (nothing more than "true" and "true" gets outputted); and saving images drops out randomly (e.g. the first time i run the app i get all 41 images on disk; the second time i get 3-4 on disk, etc). Strange. I appreciate any suggestions!
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); camera = new QCamera(this); viewfinder = new QCameraViewfinder(this); camera->setViewfinder(viewfinder); imageCapture = new QCameraImageCapture(camera); connect(camera, SIGNAL(locked()), this, SLOT(my_camera_lock())); connect(camera, SIGNAL(lockFailed()), this, SLOT(my_lock_failed())); qDebug() << connect(camera, SIGNAL(locked()), this, SLOT(my_camera_lock())); qDebug() << connect(camera, SIGNAL(lockFailed()), this, SLOT(my_lock_failed())); ui->verticalLayout->addWidget(viewfinder); viewfinder->show(); camera->start(); counter = 0; myTimer = new QTimer(this); connect(myTimer, SIGNAL(timeout()), this, SLOT(doCapture())); myTimer->start(4000); } void MainWindow::doCapture() { QString path = "/Users/JCRohner/Junk/image" + QString::number(counter) + ".jpg"; QApplication::beep(); camera->searchAndLock(); imageCapture->capture(path); camera->unlock(); counter++; if(counter == 40) myTimer->stop(); } void MainWindow::my_camera_lock(){ qDebug()<<"locked"; } void MainWindow::my_lock_failed(){ qDebug()<<"lock failed"; } MainWindow::~MainWindow() { delete ui; }
-
@JRohner You need to somehow find out where it actually freezes. You can add some qDebug() << "..." debug output for that.