I am trying to emit standard string with qt signal. The signal will be delivered as queued. I registered the type with qRegisterMetaType , like it says in the qt documentation, but no luck. I am registering it like this
qRegisterMetaType<std::string>("std::string")
emit std::string with qt signal
You should also do:
Q_DECLARE_METATYPE (std::string)
Quoting Qt Doc
Adding a Q_DECLARE_METATYPE() makes the type known to all template based functions, including QVariant. Note that if you intend to use the type in queued signal and slot connections or in QObject's property system, you also have to call qRegisterMetaType() since the names are resolved at runtime.
I tried, still doesn't work. Also I have many signals in my project that are emitting non qt types, and never did Q_DECLARE_METATYPE on them,only qRegisterMetaType, and they are working. –
Cabral
I just did a quick test, and using both the qRegisterMetaType along with Q_DECLARE_METATYPE does indeed work. Leaving out either one results in a run-time error message about needing to call qRegisterMetaType. My guess is that for the other types you're using you're not using a Queued connection. –
Cellini
ok , I was using this overload void qRegisterMetaTypeStreamOperators ( const char * typeName ) maybe that why it didn't work.When tried this overlaod int qRegisterMetaType () with combination of Q_DECLARE_METATYPE it works, so Thank you very much. –
Cabral
I am still wondering, why my other connections in my project , for which I am sure that are queued, are working only with qRegisterMetaTypeStreamOperators ( const char * typeName ), and no need for Q_DECLARE_METATYPE macro. This brings the question which version of qRegisterMetaType to use, when working with queued signals? –
Cabral
I was worried that it didn't work at your first comment so I am now quite reliefed. Glad it works. Sometimes the Qt meta type system works like magic. I think it is best to ask about the intrinsics on the #qt channel at Freenode. –
Unboned
Thank you again. I think that probably the right answer is to use int qRegisterMetaType () with combination with Q_DECLARE_METATYPE since it explicitly mentions queued connection in the explanation. –
Cabral
What about in qt5 ? I have no luck with your sollution. What EXACLY should we write in that function contents? –
Slaveholder
I realize that this actually works when more than one signal of different types is sent, e.g. an int and a std::string. Following this logic, the following code may not work:
connect(senderObject.get(), &senderObject::signal, this, [this](std::string str_value){function(str_value)});
but this works
connect(senderObject.get(), &senderObject::signal, this, [this](int int_val, std::string str_value){function(int_val,str_value)});
Maybe there is an overload template that allows this, but if only one string is passed, it is better to use QString directly
© 2022 - 2024 — McMap. All rights reserved.