如何发送包含 cv::Mat 的 Qt 信号? [英] How to send a Qt signal containing a cv::Mat?

查看:179
本文介绍了如何发送包含 cv::Mat 的 Qt 信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我收到以下错误:

In short, I get following error:

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

我想要做的是将包含两个 cv::Mat 图像的信号从 QThread 发送到主线程,以便我可以显示输出.没有编译时错误,但是当我运行程序时,它卡在 qglobal.h 的断点处(inline void qt_noop() {}).

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

我尝试将 Q_DECLARE_METATYPE(cv::Mat) 添加到代码中,但无济于事.我现在不知道该做什么.

I've tried to add Q_DECLARE_METATYPE(cv::Mat) to the code, to no avail. I'm quite suck with what to do now.

在 QThread 类中:

In a QThread class:

signals:
void sndFlow(cv::Mat &leftEye, cv::Mat &rightEye);

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

在 QObject 类中捕获:

Capturing in a 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);
}

主要内容:

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

将信号槽变量更改为 QSharedPointer<;cv::Mat > 也不起作用.给出同样的错误:

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().)

作品

好吧,看来可以了.我已经移动 qRegisterMetaType<cv::Mat >("cv::Mat"); 就在 QObject::connect 调用之前.但是我仍然需要'F5'越过qglobal.h 中的断点,之后它就可以工作了.

WORKS

All right, it seems to work. I've move qRegisterMetaType< cv::Mat >("cv::Mat"); right before the QObject::connect call. However I still have to 'F5' past the breakpoint in qglobal.h, it works afterwards.

我可能错了,但似乎 qRegisterMetaType 的位置不是微不足道的.

I might be wrong, but it seems that the location of qRegisterMetaType is not trivial.

推荐答案

需要调用 qRegisterMetaType 除了宏(或代替它,取决于您的需要).这对于信号能够跨线程编组数据是必要的.但是,如果您使用 QObject 层次结构来管理对象生命周期,则通过引用或智能指针或原始指针传递可能是一个更明智的想法.

You need to call qRegisterMetaType in addition to the macro (or instead of it, depending on your needs). This is necessary for the signals to be able to marshal your data across threads. However, it might be a wiser idea to pass by reference or smart pointer, or raw pointer if you are using the QObject hierarchy to manage the object lifetime.

这篇关于如何发送包含 cv::Mat 的 Qt 信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆