开发者

emit std::string with qt signal

I am trying to emit standard string with qt signal. The signal will be delivered as queued. I registered the type with qRegister开发者_Python百科MetaType , like it says in the qt documentation, but no luck. I am registering it like this qRegisterMetaType<std::string>("std::string")


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜