Qt_Note16_QML_Connections

MyComponent.qml

cpp 复制代码
//import QtQuick 2.0
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Rectangle{
    width: 400
    height: 300
    property Component com1
    property Component com2
    border.color: "black"

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

        Connections{
            target: loader1.item
            ignoreUnknownSignals: true
//            function onBtnSig(value){
//                console.log("right ", value)
//            }
            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{
            target: loader2.item
            ignoreUnknownSignals: true
            function onBtnSig(value){
                console.log("left ",value)
            }
        }
    }
}

main.qml

cpp 复制代码
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    id: window
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    color: "white"

    Component{
        id: com
        Button{
            id: btn
            width: 100
            height: 50
            background: Rectangle{
                anchors.fill: parent
                border.color: btn.activeFocus ? "blue" : "black"
            }

//            signal btnSig(int value)
//            onClicked: {
                console.log("123")
//                btnSig(10)
//            }
            signal leftBtnPressed()
            Keys.onLeftPressed: {
                leftBtnPressed()
            }
        }
    }

    MyComponent{
        com1: com
        com2: com
    }

//    // 信号和槽
//    signal testSig(string s, int value)

    function func(ss, ii){
        console.log(ss,ii)
    }

//    Button{
//        width: 50
//        height: 50
//        onClicked: {
//            testSig("zhangsan","99")
//        }
//    }

//    Connections{
//        target: window  //发送信号的对象 就是信号从哪里来的
        onTestSig: {
            console.log(s,value)

        }
//        function onTestSig(str, ivalue){	//on+信号首字母大写的信号名字
//            console.log("hello",str, ivalue)
//        }
//    }

    Component.onCompleted: {	//使用connect
        testSig.connect(func)
    }
}
相关推荐
&白帝&1 分钟前
vue右键显示菜单
前端·javascript·vue.js
Wannaer2 分钟前
从 Vue3 回望 Vue2:事件总线的前世今生
前端·javascript·vue.js
羽球知道22 分钟前
在Spark搭建YARN
前端·javascript·ajax
光影少年1 小时前
vue中,created和mounted两个钩子之间调用时差值受什么影响
前端·javascript·vue.js
青苔猿猿1 小时前
node版本.node版本、npm版本和pnpm版本对应
前端·npm·node.js·pnpm
一只码代码的章鱼2 小时前
Spring的 @Validate注解详细分析
前端·spring boot·算法
zimoyin2 小时前
Kotlin 协程实战:实现异步值加载委托,对值进行异步懒初始化
java·前端·kotlin
恋猫de小郭2 小时前
如何查看项目是否支持最新 Android 16K Page Size 一文汇总
android·开发语言·javascript·kotlin
名誉寒冰2 小时前
# KVstorageBaseRaft-cpp 项目 RPC 模块源码学习
qt·学习·rpc
赵大仁2 小时前
React Native 与 Expo
javascript·react native·react.js