Qt信号/插槽发送完整的结构 [英] Qt Signal/Slots sending a complete structure

查看:115
本文介绍了Qt信号/插槽发送完整的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过两个线程之间的信号/插槽发送结构,我的信号/插槽已正确连接,并且我能够发送包含部分数据的QString,但是现在我需要发送整个内容,并且似乎结构最明智的.但是,当我尝试信号未发送/接收时.问题似乎只在于发送/接收结构,我尝试过许多方法之前和之后的处理.

I am attempting to send a structure via signals/slots between two threads, my signals/slots are connected properly and I have been able to send QStrings containing parts of my data but now I need to send the whole thing and Structures seem most sensible. However when I try the signal is not sent/recieved. The problem seems to be only with sending/receiving the structure, the processing before and after I have tried many ways.

我不能在此处

I cannot use pointers such here or here as my data is generated too fast and memory gets over written or freed (I have tried with pointers and assume references will be similarly effected).

我已经在我的结构中添加了 Q_DECLARE_METATYPE .我的结构目前仅是一个小测试(稍后将进行扩展),并且位于其自己的头文件中.

I have already added the Q_DECLARE_METATYPE to my structure. My structure is only a small test for now (to be enlarged later) and is in its own header file.

#ifndef RETURNSTRUCT_H
#define RETURNSTRUCT_H

struct Datastruct
{
    int markeridone;
};

Q_DECLARE_METATYPE(Datastruct);

#endif //RETURNSTRUCT_H

为什么我的程序无法发送/接收结构?任何帮助都将不胜感激.

Why might my program be unable to send/receive structures? any help is much appreciated.

我正在使用Windows 7,MinGW 32bit,Qt 5.7.0,Qt Creator 4.0.3

I am using windows 7, MinGW 32bit, Qt 5.7.0, Qt Creator 4.0.3

推荐答案

您的调试日志应警告您-您只能发送qt元系统已知的类型.使用Q_REGISTER_METATYPE,您最终将注册与在其中进行定义的名称空间相关联的类型.

Your debug-log should warn you about it - you can only send types known to the meta-system of qt. Using Q_REGISTER_METATYPE you end up registering types associated with the namespace where the definition was made.

幸运的是,您可以像这样告诉Qt您的结构:

Fortunately you can tell Qt about your struct like this:

// after QApplication was instantiated
qRegisterMetaType<Datastruct>("Datastruct");
// but before any class is instantiated that connects signals with this type

并且它不会尝试通过查看代码来推断名称空间.确保重新运行qmake(或者最好进行清理),否则在使用QtCreator进行构建时可能会被忽略.

And it will not try to infer a namespace by looking at the code. Make sure to re-run qmake (or better yet do a clean), or it might be overlooked when building with QtCreator.

如果您稍后碰巧通过信号传递类型的模板类,请确保也注册它们,因为即使Qt知道QList,它也不知道您类型的QList:

If you later happen to pass template-classes of your types via signals, make sure to register them as well, because even if Qt knows about QList, it doesnt know about QList of your type:

qRegisterMetaType<QList<Datastruct>>("QList<Datastruct>");

另外,请注意:如果#define类别名,请确保使用其真实名称进行注册.

On another note: if you #define class aliases, make sure to register them with their real names.

#define std::shared_ptr model_ptr
// you can declare your signals like this:
void my_signal(model_ptr<my_model>);
// but have to register the type like this:
qRegisterMetaType<std::shared_ptr<my_model>>("std::shared_ptr<my_model>");

这篇关于Qt信号/插槽发送完整的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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