0

I want to use the camera in a headless (console) qt application (at least for unit testing).

But I facing a problem with Qt. As soon I use my code in a console application, the camera won't work - the readyForCaptureChanged event of QCameraImageCapture will not be called.

If I use exactly the same code in a gui application, the event gets triggered and I can capture images.

The common code I use is that:

    camera = new QCamera(cameras.at(config->cameraNumber()));

    imageCapture = new QCameraImageCapture(camera);
    connect(imageCapture, SIGNAL(readyForCaptureChanged(bool)), this, SLOT(readyForCapture(bool)));

    camera->start(); // to start the viewfinder

// ——
void ImageCapture::readyForCapture(bool b) {
    qDebug() << "ready for capture "<<b;
}
  • when I call this code in the gui application directly in the constructor of my MainWindow, it works (event will be triggered).
  • When I call this code in my qt console application, it does not work (event will not be triggered).

Can anybody help me? Thanks

** UPDATE 29. August - full code **

Console Application:

main.cpp

#include <QCoreApplication>
#include <QTest>
#include <QTimer>
#include <QDebug>

#include <runoneventloop.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    RunOnEventLoop * run = new RunOnEventLoop(&a);
    QTimer::singleShot(0, run, SLOT(run()));

    return a.exec();
}

RunOnEventLoop.cpp

#include "runoneventloop.h"

RunOnEventLoop::RunOnEventLoop(QObject *parent) :
    QObject(parent)
{
}

void RunOnEventLoop::run() {
    qDebug() << "hier run";

    camera = new QCamera(0);

        imageCapture = new QCameraImageCapture(camera);
        connect(imageCapture, SIGNAL(readyForCaptureChanged(bool)), this, SLOT(readyForCapture(bool)));

        camera->start(); // to start the viewfinder
}

void RunOnEventLoop::readyForCapture(bool b) {
    qDebug() << "ready of capture "<<b;
}

RunOnEventLoop.h

#ifndef RUNONEVENTLOOP_H
#define RUNONEVENTLOOP_H

#include <QObject>
#include <QDebug>
#include <QCamera>
#include <QCameraImageCapture>

class RunOnEventLoop : public QObject
{
    Q_OBJECT
public:
    explicit RunOnEventLoop(QObject *parent = 0);

private:
    QCamera* camera;
    QCameraImageCapture* imageCapture;

signals:

public slots:
    void run();
    void readyForCapture(bool);

};

#endif // RUNONEVENTLOOP_H

GUI Application

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qDebug() << "hier";

    camera = new QCamera(0);

        imageCapture = new QCameraImageCapture(camera);
        connect(imageCapture, SIGNAL(readyForCaptureChanged(bool)), this, SLOT(readyForCapture(bool)));

        camera->start(); // to start the viewfinder

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::readyForCapture(bool b) {
    qDebug() << "ready of capture "<<b;
}

again, it's the same code. Console App does not call the readyForCapture method, while the gui application calls it.

you can download the archive here: DOWNLOAD

appsthatmatter
  • 6,347
  • 3
  • 36
  • 40

1 Answers1

0

If would be nice if you could provide something more of your console-based Qt application... the code you presented, how is it called by your main code?

Anyway, just guessing, if no events are raised at all maybe it is because you are not running any event loop... are you sure that your code at some point call exec() on your QCoreApplication object? Are you sure that the owner of the object from which you call connect() is the thread of QCoreApplication?

Morix Dev
  • 2,700
  • 1
  • 28
  • 49
  • @jjoe64: it is not enough... I need to see how `RunOnEventLoop` is declared... can you provide the full code of that class? – Morix Dev Aug 28 '14 at 13:48