Qt发出的信号形成一个后台线程,主线程永远不会到达

时间:2014-03-27 17:11:21

标签: c++ multithreading qt opencv image-processing

我很难在后台线程和运行的主应用程序之间获得Qt :: QueuedConnection。我有一个从QObject派生的摄像头捕获类,它通过使用:

移动到QThread
m_CameraCapture.moveToThread(&m_CameraCaptureThread);

然后,我连接信号和插槽:

    //Connect error signal)
    QObject::connect(&m_CameraCapture, SIGNAL(error(QString,QString)), this, SLOT(reportError(QString,QString)));
    //Connect the finished signal of the worker class to the thread for quitting the loop
    connect(&m_CameraCapture, SIGNAL(finished()), &m_CameraCaptureThread, SLOT(quit()));
    //This connections guarantees that the *m_CVideoCapture is automatically deleted if the event loop of the thread is terminated. Therefore, m_CVideoCapture does not need to be released manually if the capturing process is stopped.
    QObject::connect(&m_CameraCaptureThread, SIGNAL(finished()), &m_CameraCaptureThread, SLOT(deleteLater()));
    QObject::connect(&m_CameraCapture, SIGNAL(finished()), &m_CameraCapture, SLOT(deleteLater()));
    //Connect sendFrame to update frame for displaying the current frame
    QObject::connect(&m_CameraCapture, SIGNAL(sendFrame(cv::Mat)), this, SLOT(receiveFrame(cv::Mat)),Qt::BlockingQueuedConnection);
    QObject::connect(this, SIGNAL(startGrabbing()), &m_CameraCapture, SLOT(startGrabbing()));
    QObject::connect(this, SIGNAL(stopGrabbing()), &m_CameraCapture, SLOT(stopGrabbing()));

m_CameraCapture对象包含一个定时器,它定期调用m_CameraCapture的grabFrame()槽。该函数定义如下:

void CCameraCapture::grabFrame(){
    QMutexLocker ml(&m_Mutex);
    qDebug() << "Elapsed time " << GetTickCount()-lastTickCount<<" ms";
    qDebug() << "Thread ID timer: " << m_Timer.thread();
    qDebug() << "Thread ID worker: " << this->thread();
    lastTickCount=GetTickCount();       
    //Local image storage
    cv::Mat cvFrameBGR;
    //Get new frame from camera
    m_Cap>>cvFrameBGR;      
    //cv::Mat cvFrameRGB;
    ////Convert frame to RGB
    //cv::cvtColor(cvFrameBGR, cvFrameRGB, CV_BGR2RGB);
    ////Convert cv::Mat to QImage
    //m_Frame=QImage((uchar*)(cvFrameRGB.data),cvFrameRGB.cols,cvFrameRGB.rows,QImage::Format_RGB888);
    //Send frame to receivers
    emit sendFrame(cvFrameBGR);
}

计时器和线程的线程ID与主应用程序的线程ID相同且不同 - 所以这似乎是正确的。这一点是

emit sendFrame(cvFrameBGR);

如果我使用

,此信号仅到达主应用程序
QObject::connect(&m_CameraCapture, SIGNAL(sendFrame(cv::Mat)), this, SLOT(receiveFrame(cv::Mat)),Qt::BlockingQueuedConnection);

在主应用程序中连接它。但这不是我想要的,因为它会减慢我的捕获循环。我想将它与Qt :: QueuedConnection连接。但是,当我使用QueuedConnection而不是BlockingQueuedConnection时,接收对象中的receiveFrame(cv :: Mat)插槽永远不会被执行(同样使用AutoConnection它也不会工作)。接收对象是从QObject派生的cameraHandler类。谢谢你的提示!

0 个答案:

没有答案