2014-08-27 125 views
0

我想在無頭(控制檯)qt應用程序中使用攝像頭(至少對於單元測試)。在Qt控制檯應用程序中使用QCamera

但我面臨Qt的問題。一旦我在控制檯應用程序中使用我的代碼,相機將無法工作 - 將不會調用事件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應用程序的代碼,它的工作原理(將觸發事件)。
  • 當我在我的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應用

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; 
} 

再次,這是相同的代碼。控制檯應用程序不調用readyForCapture方法,而gui應用程序調用它。

,你可以在這裏下載檔案:DOWNLOAD

+0

我認爲QtMultimedia取決於QtGui,所以你需要Qapplication對象。 – prajmus 2014-08-27 13:24:26

回答

0

如果將是很好,如果你可以提供更多的東西你基於控制檯的Qt應用程序的...你提供的代碼,它是如何通過你的主代碼叫什麼名字?

反正只是猜測,如果沒有事件被觸發所有可能是因爲您沒有運行任何事件循環......你確定你的代碼在你QCoreApplication對象的某些點呼叫exec()?您確定您呼叫connect()的對象的所有者是QCoreApplication的線程嗎?

+0

看到更新.... – appsthatmatter 2014-08-28 10:18:36

+0

@ jjoe64:這是不夠的...我需要看看如何聲明'RunOnEventLoop' ...你能提供該類的完整代碼嗎? – 2014-08-28 13:48:12

+0

已上傳完整代碼 – appsthatmatter 2014-09-01 14:51:54

相關問題