QML自定义带参数信号槽

在组件之间通信,在QML中使用信号槽,并且有时需要携带参数,比如下面组件:

less 复制代码
Rectangle {
    id: root
    implicitWidth: 849
    implicitHeight: 480
    color: "#ffffff"
    radius: 15
    property string title1 : qsTr("Aux Temp Zone 1")
    property string title2 : qsTr("Aux 1 Name")
    property string auxTempZoneSwitchStatus   //辅助温区Switch状态,0表示关闭,1表示开启
    signal auxTempZoneSwitchChangeSignal(string checked)    //辅助温区Switch状态变化,0表示关闭,1表示开启

在这里定义一个信号,使用signal关键字,注意这里可以使用string类型,然后在发射信号的地方直接调用即可:

scss 复制代码
onCheckedChanged: {
                    auxTempZoneSwitchChangeSignal(getSwitchStringStatus(checked))
                }

接着就可以定义处理该信号的槽函数了:

less 复制代码
AuxTempItem{
                id: item1
                width: 849
                height: 477
                anchors.top: parent.top
                anchors.horizontalCenterOffset: 1
                anchors.topMargin: 52
                anchors.horizontalCenter: parent.horizontalCenter
                auxTempZoneSwitchStatus: GCDataPool.auxiliaryHeator1_config
                Connections{
                    target: item1
                    function onAuxTempZoneSwitchChangeSignal(value) {
                        console.log("onAuxTempZoneSwitchChangeSignal AuxTempItem", value)
                        GCDataPool.auxiliaryHeator1_config = value;
                    }
                }
            }

这里使用Connections关键字,第一个参数是target指明信号发出的对象,第二个参数必须是以function开头,on+信号名+(参数) 的形式,这个形式有2点需要注意:

  • 必须是on开头,加上信号的名字,以大写开始。
  • 参数不必声明类型,比如这里的(value),比如写成(string value)

如果我们还是以lambda的方式,比如下面这种:

ini 复制代码
Connections{
                    target: item1
                    onAuxTempZoneSwitchChangeSignal: {
                        console.log("AuxTempItem", value)
                        GCDataPool.auxiliaryHeator1_config = value;
                    }
                }

就会报错:# QML Connections: Implicitly defined onFoo properties in Connections are deprecated.

相关推荐
醇醛酸醚酮酯1 小时前
Qt项目锻炼——TODO清单(二)
开发语言·数据库·qt
Mr_Xuhhh2 小时前
信号与槽的总结
java·开发语言·数据库·c++·qt·系统架构
灵性花火4 小时前
Qt的前端和后端过于耦合(0/7)
开发语言·前端·qt
菜鸟看点13 小时前
自定义Cereal XML输出容器节点
c++·qt
漫步企鹅13 小时前
【蓝牙】Linux Qt4查看已经配对的蓝牙信息
linux·qt·蓝牙·配对
new_zhou14 小时前
Windows qt打包编译好的程序
开发语言·windows·qt·打包程序
看到我,请让我去学习1 天前
Qt编程-qml操作(js,c++,canvas)
开发语言·qt
哈市雪花1 天前
相机:Camera原理讲解(使用OpenGL+QT开发三维CAD)
qt·3d·交互·相机·图形学·opengl·视角
津津有味道1 天前
Qt C++串口SerialPort通讯发送指令读写NFC M1卡
linux·c++·qt·串口通信·serial·m1·nfc
feiyangqingyun1 天前
全网唯一/Qt结合ffmpeg实现手机端采集摄像头推流到rtsp或rtmp/可切换前置后置摄像头/指定分辨率帧率
qt·智能手机·ffmpeg