2017-09-18 47 views
0

我有我的主線程負責GUI和我的工作線程負責與我的攝像頭做東西。爲什麼我的工作線程沒有收到這個信號?

我能夠從我的主線程發出一個信號到我的攝像頭線程,但它永遠不會收到它!有人可以解釋我爲什麼嗎?

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

    setThreadsAndObj(); 
    setConnects(); 
    startCamera(); 

} 

void MainWindow::setThreadsAndObj() 
{ 
    cameraClassPtr = new cameraClass(); 
    cameraThread = new QThread(); 

} 

void MainWindow::setConnects() 
{ 

    .... 

    connect(this, &MainWindow::savePicture, cameraClassPtr, &cameraClass::takePicture); 


} 


void MainWindow::startCamera() 
{ 

    //camera start 
    connect(cameraThread, &QThread::started, cameraClassPtr, &cameraClass::getVideoFrame); 
    connect(cameraThread, &QThread::finished, cameraClassPtr, &QThread::deleteLater); 
    connect(cameraThread, &QThread::finished, cameraThread, &QThread::deleteLater); 
    //receive camera frames 
    connect(cameraClassPtr, &cameraClass::videoFrameToGui, this, &MainWindow::updateCameraStream); 


    //start threads 
    cameraClassPtr->moveToThread(cameraThread); 
    cameraThread->start(); 

} 


void MainWindow::foo() 
{ 
     emit savePicture(); 
} 


void cameraClass::takePicture() 
{ 

    qDebug()<<"camera class received signal"; //THIS NEVER GETS PRINTED 

} 

有人可以解釋我爲什麼我的信號從來沒有收到和「相機類接收信號」從未打印和如何解決這個問題?

+0

嘗試更改'cameraThread = new QThread();'''cameraThread = new QThread(this);' – eyllanesc

+0

@eyllanesc有什麼區別? – traducerad

+0

@eyllanesc解決了它。我在我的工作線程中有一段時間1循環。我不得不添加QApplication :: processEvents();在while循環中。對不起,讓你失去時間。 – traducerad

回答

0

我在工作線程過這樣的循環:在while(1)循環QApplication::processEvents();,從而使事件循環不掛:

... 
static cv::VideoCapture cap(-1); 
if(!cap.isOpened()) 
{ 
    qDebug()<< "Could not initialize capturing...\n"; 
} 

qDebug()<<"starting live camera"; 
cap.set(CV_CAP_PROP_FRAME_WIDTH,240); 
cap.set(CV_CAP_PROP_FRAME_HEIGHT,320); 

while(1) 
{ 
    cap >> imgFrame; 

    int fd = open("/home/John/Desktop/mainImglock.txt", O_RDWR |O_CREAT, 0666); 
    int rc = flock(fd, LOCK_EX | LOCK_NB); 
    if(rc==0) 
    { 
     cv::imwrite("/home/John/Desktop/camera.jpg", imgFrame); 
    } 
    flock(fd, LOCK_UN); 
    close(fd); 
} 

我加入解決了這個問題。

0

您需要繼承QThread並調用QThread :: exec();爲了處理這個線程中的事件。

相關問題