简体   繁体   English

通过排队的Qt信号槽连接发送QSharedPointers的QVector

[英]Sending QVector of QSharedPointers through queued Qt signal-slot connection

I've encountered problem when I've tried to send QVector > between threads through queued signal-slot connection (Tester is my class that inherits QObject) 当我尝试通过排队的信号槽连接在线程之间发送QVector>时遇到了问题(Tester是我继承QObject的类)

When I try to simply connect signal\\slot with such QVector as argument programm tells during run that this metatype should be registered (though QVector, QSharedPointer and class inherited from QObject should be registered automatically. 当我尝试简单地将signal \\ slot连接到这样的QVector时,因为参数programm在运行期间告知应该注册此元类型(尽管QVector,QSharedPointer和从QObject继承的类应该自动注册。

I've tried adding 我试过添加

typedef QVector<QSharedPointer<Tester> > TestPointerVector;
Q_DECLARE_METATYPE(TestPointerVector);

after declaration of Tester class and add to main() 声明Tester类并添加到main()之后

qRegisterMetaType<TestPointerVector>();

In that case I have compiling errors in qmetatype.h и qtypeinfo.h telling me that QVector > is incomplete type and that sizeof of such vector can't be calculated. 在那种情况下,我在qmetatype.h和qtypeinfo.h中编译错误,告诉我QVector>是不完整的类型,并且无法计算这种向量的sizeof。

What is right way to do this register metatype thing? 什么是正确的方法来做这个注册metatype的事情?

My testing main() looks as follow: 我的测试main()如下所示:

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qRegisterMetaType<TestPointerVector>();
    Receiver* rcv = new Receiver(0);
    Emitter* emt = new Emitter(0);
    QThread* thread = new QThread();
    QObject::connect(rcv, SIGNAL(finished()), thread, SLOT(quit()));
    QObject::connect(rcv, SIGNAL(finished()), rcv, SLOT(deleteLater()));
    QObject::connect(emt, SIGNAL(send(TestPointerVector)), rcv, 
    SLOT(receive(TestPointerVector)), Qt::QueuedConnection);
    rcv->moveToThread(thread);
    thread->start();
    emt->process();

    while(!thread->isFinished())
        continue;
    return a.exec();
}

Found problem, it wasn't in qRegisterMetaType part. 发现问题,它不在qRegisterMetaType部分。 Problem was that I was using Qvector<...> in signal\\slot signatures, I've changed it to const QVector<...>& and everything started working. 问题是我在信号\\插槽签名中使用Qvector <...>,我已将其更改为const QVector <...>&并且一切都开始工作了。

And if QSharedPointer is deleted in emitter thread it'd still be received by receiver and data won't be corrupted (what I was trying to test by this example). 如果在发射器线程中删除了QSharedPointer,它仍然会被接收器接收,并且数据不会被破坏(我试图通过这个例子测试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM