Ayb*_*gür 6 c++ qt templates signals-slots
我可以以任何方式使用模板类型作为插槽或信号参数吗?作为一个例子,我试图定义以下内容:
void exampleSignal(std::map<non_template_type_1,non_template_type_2> arg);
void exampleSlot(std::map<non_template_type_1,non_template_type_2> arg);
Run Code Online (Sandbox Code Playgroud)
这会在运行时导致以下结果:
QObject::connect: Cannot queue arguments of type
'std::map<non_template_type_1,non_template_type_2>'
(Make sure 'std::map<non_template_type_1,non_template_type_2>'
is registered using qRegisterMetaType().)
Run Code Online (Sandbox Code Playgroud)
试图注册std::map<non_template_type_1,non_template_type_2>
与Q_DECLARE_METATYPE()
在编译失败的结果,显然不支持.
作为一种解决方法,我正在使用QVariantMap
而不是std::map
.但我真的想知道解决这个问题的正确方法; 一个无法修改模板类的地方.
编辑:我忘了提到信号和插槽是在不同的线程中发出和接收的.显然,单线程方案中不会发生运行时错误.