How to use template types as slot and signal parameters in several streams?

Can the type of the template be used in any way as an argument to a slot or signal? As an example, I am trying to determine 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); 

As a result, at runtime, this leads to the following:

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

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

As a workaround, I use QVariantMap instead of std::map . But I really would like to know the right way to solve this problem; where it is impossible to change the template classes.

Edit: I forgot to mention that the signal and slot are emitted and received in different streams. Apparently, a runtime error does not occur in single-thread scripts.

+6
source share
3 answers

As explained in this thread , you can try to use a typedef , including the QMetaType header, and then use both the Q_DECLARE_METATYPE macro and qRegisterMetaType (as implied by this thread for a similar problem).

+2
source

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>" ); 
+2
source

There is no problem if you created such a class and used the Qt moc compiler to automatically create these QMetaObject :

 class MyClass : public QObject { Q_OBJECT public: explicit MyClass(QObject *parent = 0) : QObject(parent) { } public slots: void exampleSlot(std::map<non_template_type_1,non_template_type_2> arg); signals: void exampleSignal(std::map<non_template_type_1,non_template_type_2> arg); }; 

Of course, you need to enable QObject wherever std::map .

+1
source

Source: https://habr.com/ru/post/976726/


All Articles