How to send a Qt signal containing cv :: Mat?

In short, I get the following error:

QObject::connect: Cannot queue arguments of type 'cv::Mat' (Make sure 'cv::Mat' is registered using qRegisterMetaType().) 

What I'm trying to do is send a signal containing two cv :: Mat images from QThread to the main thread so that I can display the output. There is no compile-time error, but when I run the program, it gets stuck at the breakpoint in qglobal.h ( inline void qt_noop() {} ).

I tried adding Q_DECLARE_METATYPE(cv::Mat) to the code, but to no avail. I'm pretty suck what to do now.

the code

In the QThread class:

 signals: void sndFlow(cv::Mat &leftEye, cv::Mat &rightEye); void eyesDriver::run() { forever { flow->draw(leftEye, rightEye); sndFlow(leftEye, rightEye); } } 

Capture in the QObject class:

 public slots: void recFlow(cv::Mat &leftEye, cv::Mat &rightEye); void myClass::recFlow(cv::Mat &leftEye, cv::Mat &rightEye) { cv::imshow("left", leftEye); cv::imshow("rigth", rightEye); cv::waitKey(40); } 

Mostly:

 Q_DECLARE_METATYPE(cv::Mat) int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qRegisterMetaType< cv::Mat >("cv::Mat"); // create objects from QThread and QObject class QObject::connect(&qthread, SIGNAL(sndFlow(cv::Mat&,cv::Mat&)), &qobject, SLOT(recFlow(cv::Mat&,cv::Mat&))); qthread.start(); return a.exec(); } 

Changing the signal slot variables to QSharedPointer< cv::Mat > does not work either. Gives the same error:

 QObject::connect: Cannot queue arguments of type 'QSharedPointer<cv::Mat>' (Make sure 'QSharedPointer<cv::Mat>' is registered using qRegisterMetaType().) 

Work

Good, it seems to work. I move qRegisterMetaType< cv::Mat >("cv::Mat"); right before calling QObject::connect . However, I still need β€œF5” to go through the breakpoint in qglobal.h, after which it works.

Maybe I'm wrong, but it seems that the qRegisterMetaType not trivial.

+8
qt opencv signals-slots qthread
source share
1 answer

You need to call qRegisterMetaType in addition to the macro (or instead, depending on your needs). This is necessary so that signals can march your data on streams. However, it might be a wiser idea to pass by reference or smart pointer or raw pointer if you use the QObject hierarchy to control the lifetime of an object.

+3
source share

All Articles