0

ヘッドレス(コンソール)qtアプリケーションでカメラを使用したい(少なくとも単体テスト用)。

しかし、Qtで問題に直面しています。コンソール アプリケーションでコードを使用するとすぐに、カメラが機能しなくなり、readyForCaptureChangedイベントQCameraImageCaptureが呼び出されなくなります。

GUI アプリケーションでまったく同じコードを使用すると、イベントがトリガーされ、画像をキャプチャできます。

私が使用する一般的なコードは次のとおりです。

    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;
}
  • GUI アプリケーションでこのコードを MainWindow のコンストラクターで直接呼び出すと、機能します (イベントがトリガーされます)。
  • Qt コンソール アプリケーションでこのコードを呼び出すと、機能しません (イベントはトリガーされません)。

誰でも私を助けることができますか?ありがとう

** 8 月 29 日更新 - 完全なコード **

コンソール アプリケーション:

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 アプリケーション

メインウィンドウ.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;
}

繰り返しますが、それは同じコードです。コンソール アプリは readyForCapture メソッドを呼び出しませんが、GUI アプリケーションはそれを呼び出します。

ここからアーカイブをダウンロードできます: DOWNLOAD

4

1 に答える 1

0

コンソールベースの Qt アプリケーションをもう少し提供できればいいのですが...あなたが提示したコードは、メインコードからどのように呼び出されますか?

とにかく、推測ですが、イベントがまったく発生しない場合は、イベント ループを実行していないことが原因である可能性があります...ある時点でコードがオブジェクトを呼び出しexec()ていると確信していますか? QCoreApplication呼び出し元のオブジェクトの所有者connect()が のスレッドであることは確かQCoreApplicationですか?

于 2014-08-27T11:50:59.070 に答える