求助, qt 信号槽到底怎么传递 std::string 啊, SLOTS 始终调不到。 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fyyz
V2EX    C++

求助, qt 信号槽到底怎么传递 std::string 啊, SLOTS 始终调不到。

  •  
  •   fyyz 2020-03-05 13:13:46 +08:00 4043 次点击
    这是一个创建于 2109 天前的主题,其中的信息可能已经有所发展或是发生改变。

    窗口 class 头文件是这么写的:

    Q_DECLARE_METATYPE(std::string) class A: public QMainWindow { Q_OBJECT A() { qRegisterMetaType<std::string>("std::string"); QObject::connect( this, SIGNAL(tunnel::add_iteminto_table_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。

    12 条回复    2020-03-06 10:12:26 +08:00
    1KN6sAqR0a57no6s
        1
    1KN6sAqR0a57no6s  
       2020-03-05 13:20:12 +08:00
    试试 QString 呢
    fyyz
        2
    fyyz  
    OP
       2020-03-05 13:30:17 +08:00
    @YuxiangLuo 我更想知道我哪里不对,因为我看别人连自己的 struct 都能传递过去。
    benson458
        3
    benson458  
       2020-03-05 13:36:51 +08:00
    把"tunnel::" 去掉试试
    ipwx
        4
    ipwx  
       2020-03-05 13:36:56 +08:00
    @fyyz Qt 的 type 要借助预编译器的吧。。。std::string 真的能被支持么?关键是用了 template。struct 等同于 class,qmake 至少支持 customized class。不过 qmake 对 template 支持极差我记得。
    sc3263
        5
    sc3263  
       2020-03-05 13:39:22 +08:00
    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
    的日志
    sc3263/td>
        6
    sc3263  
       2020-03-05 13:39:47 +08:00
    以及。。。2020 年了
    fyyz
        7
    fyyz  
    OP
       2020-03-05 13:39:59 +08:00
    @wbing tunnel 就是 class A,刚刚复制黏贴代码后忘记改了。
    sc3263
        8
    sc3263  
       2020-03-05 13:40:42 +08:00   1
    @sc3263 试一下 Qt5 里新的信号槽连接方式呗。那个会有编译期的类型检查。
    fyyz
        9
    fyyz  
    OP
       2020-03-05 13:45:10 +08:00
    @sc3263 谢谢,用了函数指针就能找到了,以后我就这么写了~
    vx2018
        10
    vx2018  
       2020-03-05 13:46:24 +08:00
    QObject::connect(this, &tunnel::add_item_into_table_signal, this, &tunnel::add_item_into_table);
    这样不好吗? 非要用 SLOT 和 SIGNAL 宏.
    ztcaoll222
        11
    ztcaoll222  
       2020-03-05 13:51:20 +08:00
    我记得信号槽能发的类型是有限制的吧,自定义类要继承 QObject
    https://doc.qt.io/qt-5/signalsandslots.html
    dbskcnc
        12
    dbskcnc  
       2020-03-06 10:12:26 +08:00
    完成没问题的,不过需要多做点处理,
    以下是正确答案:

    一般在 main.cpp 里面加

    qRegisterMetaType<std::string>();
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2027 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 00:55 PVG 08:55 LAX 16:55 JFK 19:55
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86