qt:使用信号槽机制传参

在本文的前一章,我介绍了信号槽机制的用法,现在来介绍如何利用信号槽机制传参

信号槽机制用法:http://t.csdnimg.cn/6t2rM

定义发送端为send,接收端为receive

在send.h中:(send_message_2为带参数的信号)

cpp 复制代码
signals:
    //发出的信号写在这
    void send_message();
    void send_message_2(QString str1,QString str2);

在Public中定义发送函数

cpp 复制代码
//触发发送信号的函数
void send_function();

在send.cpp中实现函数

cpp 复制代码
//这个函数就是触发信号的函数
void send::send_function()
{
    emit send_message();
    emit send_message_2("123","456");
}

这里的"123","456"大家可以自定为任何自己想要的数据和类型

然后可以绑定个事件触发发送函数,例如

cpp 复制代码
//触发发送信号的函数
connect(send_button,&QPushButton::clicked,[=](){
    send_function();
});

接下来写接收端的代码

在receive.h中定义接收函数(receive_message_2为带参数的接收函数)

cpp 复制代码
private slots:
    //接收信号的函数在这
    void receive_message();

    //带参数的接收函数
    void receive_message_2(QString message,QString message2);

在receive.cpp中实现接收函数

cpp 复制代码
//接受带参数的函数
void receive::receive_message_2(QString message, QString message2)
{
    qDebug()<<message<<message2;
}

接下来我们回到send.cpp中,先来用receive创造一个窗体,再绑定型号和槽

cpp 复制代码
receive * wid = new receive;

//绑定信号和槽
connect(this,SIGNAL(send_message()),wid,SLOT(receive_message()));

connect(this,SIGNAL(send_message_2(QString,QString)),wid,SLOT(receive_message_2(QString,QString)));

项目运行--->触发函数--->信号触动槽--->接收到参数

以上用例的网盘地址:

链接:https://pan.baidu.com/s/1dpJmRgnGMYSSQtfv9W_77A?pwd=efdu

提取码:efdu

相关推荐
用户805533698035 小时前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner6 小时前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz5 天前
QML Hello World 入门示例
qt
xcyxiner8 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner9 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner9 天前
DicomViewer (添加模型类)3
qt
xcyxiner10 天前
DicomViewer (目录调整) 2
qt
xcyxiner10 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能12 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G12 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt