I have difficulties to get a Qt::QueuedConnection between a background thread and my main application running. I have a camera capturing class derived from QObject which is moved to a QThread by using:
m_CameraCapture.moveToThread(&m_CameraCaptureThread);
Afterwards, I connect the signals and slots:
//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()));
The m_CameraCapture object contains a timer which calls in regular intervals the grabFrame() slot of m_CameraCapture. The function is defined by:
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);
}
The thread IDs of timer and thread are the same and different to the thread ID of the main application - so this seems to be correct. The point is the line
emit sendFrame(cvFrameBGR);
This signal only arrives at the main application if I use
QObject::connect(&m_CameraCapture, SIGNAL(sendFrame(cv::Mat)), this, SLOT(receiveFrame(cv::Mat)),Qt::BlockingQueuedConnection);
in the main application to connect it. But this is not what I want because it will slow down my capturing loop. I would like to connect it with a Qt::QueuedConnection. However, when I use a QueuedConnection instead of the BlockingQueuedConnection the receiveFrame(cv::Mat) slot in the receiving Object is never executed (Also with AutoConnection it wont work). The receiving Object is a cameraHandler class which is derived from QObject. Thanks for any hint!