
窗口 class 头文件是这么写的:
Q_DECLARE_METATYPE(std::string) class A: public QMainWindow { Q_OBJECT A() { qRegisterMetaType<std::string>("std::string"); QObject::connect( this, SIGNAL(tunnel::add_item_into_tabe_signal(std::string, std::string, std::string)), this, SLOT(tunnel::add_item_into_table(std::string, std::string, std::string)) ); } signals: // 这个函数的实现是由 Qmake 生成的,我能调试单步步进进去,看到实现里的代码跑到了 void add_item_into_table_signal(std::string ip, std::string port, std::string remark); public slots: void add_item_into_table(std::string ip, std::string port, std::string remark) { DebugBreak(); // 这里跑不到 } } 已经搞了一天了,我 emit 其他没有参数的信号函数都能跑到 slots,但是这个传了参数的函数就是跑不到 slots。
1 1KN6sAqR0a57no6s 2020 年 3 月 5 日 试试 QString 呢 |
2 fyyz OP @YuxiangLuo 我更想知道我哪里不对,因为我看别人连自己的 struct 都能传递过去。 |
3 benson458 2020 年 3 月 5 日 把"tunnel::" 去掉试试 |
4 ipwx 2020 年 3 月 5 日 @fyyz Qt 的 type 要借助预编译器的吧。。。std::string 真的能被支持么?关键是用了 template。struct 等同于 class,qmake 至少支持 customized class。不过 qmake 对 template 支持极差我记得。 |
5 sc3263 2020 年 3 月 5 日 QObject::connect( this, SIGNAL(tunnel::add_item_into_table_signal(std::string, std::string, std::string)), this, SLOT(tunnel::add_item_into_table(std::string, std::string, std::string)) ); 去掉这边的 tunnel:: 如果还不行的话,调试模式启动,看看 Qt 本身有没有输出类似 QObject::connect: No such signal A::add_item_into_table_signal(std::string, std::string, std::string) in xxx.cpp 的日志 |
6 sc3263 2020 年 3 月 5 日 以及。。。2020 年了 |
10 vx2018 2020 年 3 月 5 日 QObject::connect(this, &tunnel::add_item_into_table_signal, this, &tunnel::add_item_into_table); 这样不好吗? 非要用 SLOT 和 SIGNAL 宏. |
11 ztcaoll222 2020 年 3 月 5 日 我记得信号槽能发的类型是有限制的吧,自定义类要继承 QObject https://doc.qt.io/qt-5/signalsandslots.html |
12 dbskcnc 2020 年 3 月 6 日 完成没问题的,不过需要多做点处理, 以下是正确答案: 一般在 main.cpp 里面加 qRegisterMetaType<std::string>(); |