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)
    }
}
相关推荐
拖孩5 分钟前
【Nova UI】十二、打造组件库之按钮组件(上):迈向功能构建的关键一步
前端·javascript·vue.js
独立开阀者_FwtCoder7 分钟前
用 pnpm + workspace + changesets 打造monorepo工程,【前端er必看!】
前端·javascript·面试
想回家的一天21 分钟前
雪花算法生成int64,在前端js的精度问题
开发语言·前端·javascript
.又是新的一天.22 分钟前
03_JavaScript
开发语言·javascript·ecmascript
yinzhiqing34 分钟前
ubuntu24设置拼音输入法,解决chrome不能输入中文
前端·数据库·chrome
知识分享小能手35 分钟前
JavaScript学习教程,从入门到精通,XMLHttpRequest 与 Ajax 请求详解(25)
开发语言·javascript·学习·ajax·前端框架·css3·html5
PythonPioneer41 分钟前
每日Html 4.24
前端·html
洛克希德马丁43 分钟前
关于Qt对Html/CSS的支持
css·qt·html
小小小小宇43 分钟前
H5秒开且不影响版本更新
前端
我有一棵树1 小时前
元素滚动和内容垂直居中同时存在,完美的 html 元素垂直居中的方法flex + margin: auto
前端·html