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)
    }
}
相关推荐
jllws139 分钟前
Qt学习及使用_第1部分_认识Qt---学习目的及技术准备
qt·c++框架
Uyker1 小时前
从零开始制作小程序简单概述
前端·微信小程序·小程序
到点就困告1 小时前
海康工业相机SDK二次开发(VS+QT+海康SDK+C++)
数码相机·qt·海康
Dontla4 小时前
为什么React列表项需要key?(React key)(稳定的唯一标识key有助于React虚拟DOM优化重绘大型列表)
javascript·react.js·ecmascript
EndingCoder5 小时前
React从基础入门到高级实战:React 实战项目 - 项目三:实时聊天应用
前端·react.js·架构·前端框架
阿阳微客6 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
德育处主任Pro7 小时前
『React』Fragment的用法及简写形式
前端·javascript·react.js
CodeBlossom7 小时前
javaweb -html -CSS
前端·javascript·html
CodeCraft Studio7 小时前
【案例分享】如何借助JS UI组件库DHTMLX Suite构建高效物联网IIoT平台
javascript·物联网·ui
打小就很皮...7 小时前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序