用qt信号发出std::string
emit std::string with qt signal
我正试图用qt信号发出标准字符串。信号将作为队列传递。我注册了qRegisterMetaType的类型,就像它在qt文档中所说的,但没有运气。我是这样登记的qRegisterMetaType<std::string>("std::string")
你还应该这样做:
Q_DECLARE_METATYPE (std::string)
引用Qt Doc
添加Q_DECLARE_METATYPE()使该类型为所有模板所知基于的函数,包括QVariant。如果您打算使用,请注意队列信号和槽连接或QObject的类型属性系统,,您还必须调用qRegisterMetaType(),因为名称在运行时解析。
我意识到当发送多个不同类型的信号时,例如int和std::string,这实际上是有效的。按照这个逻辑,下面的代码可能不能工作:
connect(senderObject.get(), &senderObject::signal, this, [this](std::string str_value){function(str_value)});
但这是有效的
connect(senderObject.get(), &senderObject::signal, this, [this](int int_val, std::string str_value){function(int_val,str_value)});
可能有一个重载模板允许这样做,但如果只传递一个字符串,最好直接使用QString
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- 使用 std::string () const 函数启动线程或未来
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 如何更改大小(std::string)
- std::string 的对象真的可以移动吗?
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- 真的没有来自 std::string_view 的 std::string 的显式构造函数吗?
- 将C++ std::string 转换为 UTF-16-LE 编码的字符串
- 重载 + 自己的类和 std::string 的运算符
- 如何使用 std::string 作为 QHash 的键?
- 将日语 wstring 转换为 std::string
- 可以从std::string继承以提供类型一致性吗
- 构造函数采用std::string_view与std::string并移动
- 在共享缓冲区内存中创建 ::std::string 对象
- std::string.size() 未知行为
- Valgrind 在 std::string::swap 中报告 SIGILL