Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use template types as slot and signal parameters in multiple threads?

Can I use a template type in any way as a slot or signal argument? As an example, I'm trying to define the following:

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

This results in the following during runtime:

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

Trying to register std::map<non_template_type_1,non_template_type_2> with Q_DECLARE_METATYPE() results in compilation failure and apparently is not supported.

As a workaround, I'm using QVariantMap instead of std::map. But I really would like to know the correct way to solve this problem; one where it is not possible to modify the template classes.

Edit: I forgot to mention that the signal and slot are emitted and received in different threads. Apparently the runtime error doesn't occur in single-thread scenarios.

like image 972
Ayberk Özgür Avatar asked Oct 14 '14 14:10

Ayberk Özgür


2 Answers

This works for me:

qRegisterMetaType< std::vector<float> >( "std::vector<float>" );
qRegisterMetaType< std::vector<int>   >( "std::vector<int>"   );
qRegisterMetaType< std::map<std::string,int64_t> >( "std::map<std::string,int64_t>" );
like image 121
Diego Sánchez Avatar answered Oct 21 '22 13:10

Diego Sánchez


As explained in this thread you can try using a typedef, including the QMetaType header and then using both Q_DECLARE_METATYPE macro and the qRegisterMetaType function (as implied by this thread on a similar issue).

like image 36
BartoszKP Avatar answered Oct 21 '22 13:10

BartoszKP