QML学习之QML信号和槽,自定义组件和信号槽的使用

QML-信号与槽

第一种方式

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5                   //导入Controls 2.0模块
//import QtQuick.Controls 1.4 as QC14            //导入Controls 1.4模块,取别名QC14
//import QtQuick.Controls.Styles 1.4 as QCS14    //导入Controls.Styles 1.4模块,取别名QCS14

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("My QML")

    //自定义信号
    signal testSig(string s,int value)
    //自定义函数
    function func(ss,ii){
        console.log(ss,ii)
    }

    Button{
        width: 100
        height: 50
        //发送信号
        onClicked: {
            testSig("张三",23)
        }
    }

    //组件创建完成以后去接收这个信号
    Component.onCompleted: {
        //信号+绑定(后面跟自己定义的函数方法或者是回调函数)
        testSig.connect(func)
    }
}

第二种方式(使用Connections QML Type)//这种方法没有成功,但是我看视频成功了,不清楚是不是Qt已经弃用了这种方法,推荐使用第三种方式。

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5                   //导入Controls 2.0模块
//import QtQuick.Controls 1.4 as QC14            //导入Controls 1.4模块,取别名QC14
//import QtQuick.Controls.Styles 1.4 as QCS14    //导入Controls.Styles 1.4模块,取别名QCS14

Window {
    id:window
    visible: true
    width: 640
    height: 480
    title: qsTr("My QML")

    //自定义信号
    signal testSig(string s,int value)
    //自定义函数
    function func(ss,ii){
        console.log(ss,ii)
    }

    Button{
        width: 100
        height: 50
        //发送信号
        onClicked: {
            testSig("张三",23)
        }
    }

    //可以认为是一个组件,有三个属性(enabled : bool
    //enabled : bool
    //ignoreUnknownSignals : bool
    //target : Object
    Connections {
        //发送信号的对象是谁
        target: window
        //写一个函数on+信号(信号的首字母需要大写)触发这个槽函数
        onTestSig: {
            console.log(s,value)
        }
    }
}

第三种直接在Connections 写一个function方法

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5                   //导入Controls 2.0模块
//import QtQuick.Controls 1.4 as QC14            //导入Controls 1.4模块,取别名QC14
//import QtQuick.Controls.Styles 1.4 as QCS14    //导入Controls.Styles 1.4模块,取别名QCS14

Window {
    id:window
    visible: true
    width: 640
    height: 480
    title: qsTr("My QML")

    //自定义信号
    signal testSig(string s,int value)
    //自定义函数
    function func(ss,ii){
        console.log(ss,ii)
    }
    Button{
        width: 100
        height: 50
        //发送信号
        onClicked: {
            testSig("张三",23)
        }
    }

    //可以认为是一个组件,有三个属性(enabled : bool
    //enabled : bool
    //ignoreUnknownSignals : bool
    //target : Object
    Connections {
        //发送信号的对象是谁
        target: window
        //写一个函数on+信号(信号的首字母需要大
        //直接写一个function
        function onTestSig(str,iValue){
            console.log(str,iValue)
        }
    }
}

上面的二种方式在实际的工作中会用到第二种方式的第二种方法,上面这种

自定义组件与信号槽的使用

//main.qml
import QtQuick 2.7
import QtQuick.Window 2.7
import QtQuick.Controls 2.5                   //导入Controls 2.0模块
import QtQuick.Controls 1.4 as QC14            //导入Controls 1.4模块,取别名QC14
import QtQuick.Controls.Styles 1.4 as QCS14    //导入Controls.Styles 1.4模块,取别名QCS14

Window {
    id:window
    visible: true
    width: 640
    height: 480
    title: qsTr("My QML")

    Component{
        id: com
        Button{
            //如果需要在自定义的控件当中去处理这个事件(定义信号+发送信号)
            signal btnSig(int value)
            onClicked: {
//                console.log("123")
                btnSig(10)
            }
        }
    }

    MyComponent {
        com1: com
        com2: com
    }
}
//MyComponent.qml
import QtQuick 2.7
import QtQuick.Controls 2.5

Rectangle{
    id: rect
    width: 400
    height: 300
    //外部传Component进来
    property Component com1
    property Component com2
    border.color: "red"

    Loader {
        id: loader1
        sourceComponent:com1
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 20
        anchors.right: parent.right
        anchors.rightMargin: 20

        //怎么处理传过来的信号?
        Connections{
            //这里为什么是loader1.item,当前loader1加载的项就是这个item
            //我们不需要知道传递过来的信号是什么控件,我只需要知道是这个loader1的item会发送信号过来
            target: loader1.item
            onBtnSig: {
                console.log("right" + value)
            }
        }
    }
    
    Loader {
        id: loader2
        sourceComponent:com2
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 20
        anchors.right: parent.right
        anchors.rightMargin: 150
        Connections{
            //这里为什么是loader1.item,当前loader1加载的项就是这个item
            //我们不需要知道传递过来的信号是什么控件,我只需要知道是这个loader1的item会发送信号过来
            target: loader2.item
            ignoreUnknownSignals: true
            function onBtnSig(value){
                console.log("left " + value)
            }
        }
    }
}

补充键盘事件获取焦点(按左右切换焦点)

Keys.onRightPressed:{
xxx.focus = ture
}
这里不知道谁需要focus,组件时候也不知道给谁,这时候需要定义一个信号。

//main.qml
import QtQuick 2.12
import QtQuick.Window 2.7
import QtQuick.Controls 2.5                  
Window {
    id:window
    visible: true
    width: 640
    height: 480
    title: qsTr("My QML")

    Component{
        id: com
        Button{
            id: btn
            width: 100
            height: 50
            //如果需要在自定义的控件当中去处理这个事件(定义信号+发送信号)
            background: Rectangle {
                anchors.fill: parent
                border.color: btn.activeFocus ? "blue" : "black"
            }

            signal leftBtnPressed()
            Keys.onLeftPressed: {
                leftBtnPressed()
            }
        }
    }

    MyComponent {
        com1: com
        com2: com
    }
}
//MyComponent.qml
import QtQuick 2.7
import QtQuick 2.12
Rectangle{
    id: rect
    width: 400
    height: 300
    //外部传Component进来
    property Component com1
    property Component com2
    border.color: "red"

    Loader {
        id: loader1
        sourceComponent:com1
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 20
        anchors.right: parent.right
        anchors.rightMargin: 20

        //怎么处理传过来的信号?
        Connections{
            //这里为什么是loader1.item,当前loader1加载的项就是这个item
            //我们不需要知道传递过来的信号是什么控件,我只需要知道是这个loader1的item会发送信号过来
            target: loader1.item
            function onLeftBtnPressed(){
                loader2.item.focus = true
                loader2.item.forceActiveFocus()
            }

        }
        Component.onCompleted: {
            loader1.item.focus = true
            loader1.item.forceActiveFocus()
        }
    }

    Loader {
        id: loader2
        sourceComponent:com2
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 20
        anchors.right: parent.right
        anchors.rightMargin: 150
        Connections{
            //这里为什么是loader1.item,当前loader1加载的项就是这个item
            //我们不需要知道传递过来的信号是什么控件,我只需要知道是这个loader1的item会发送信号过来
            target: loader2.item
            function onBtnSig(value){
                console.log("left " + value)
            }
        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值