Qt_Note16_QML_Connections

MyComponent.qml

//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

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. day10-16:Spring Security

    2024-04-01 11:42:05       8 阅读
  2. Ubuntu16-18网卡配置

    2024-04-01 11:42:05       8 阅读
  3. <span style='color:red;'>12</span>.<span style='color:red;'>11</span>

    12.11

    2024-04-01 11:42:05      42 阅读
  4. <span style='color:red;'>12</span>.<span style='color:red;'>11</span>

    12.11

    2024-04-01 11:42:05      38 阅读
  5. 12.15

    2024-04-01 11:42:05       47 阅读
  6. gitlab-ee 13.x->14.x->15.x->16.x升级踩坑记

    2024-04-01 11:42:05       31 阅读
  7. solidity(16

    2024-04-01 11:42:05       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-01 11:42:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-01 11:42:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-01 11:42:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-01 11:42:05       20 阅读

热门阅读

  1. 如何在Linux shell脚本中提示Yes/No/Cancel输入

    2024-04-01 11:42:05       14 阅读
  2. 2024系统架构师---隐式调用架构风格的概念与应用

    2024-04-01 11:42:05       15 阅读
  3. 2024系统架构师---黑板架构风格的概念与应用

    2024-04-01 11:42:05       20 阅读
  4. 力扣爆刷第106天之CodeTop100五连刷16-20

    2024-04-01 11:42:05       16 阅读
  5. Python爬虫之异步爬虫

    2024-04-01 11:42:05       15 阅读
  6. 数据仓库作业三:第5章 联机分析处理技术

    2024-04-01 11:42:05       14 阅读
  7. vscode使用技巧

    2024-04-01 11:42:05       17 阅读
  8. [python][whl]rknn_toolkit_lite2的whl版本下载地址汇总

    2024-04-01 11:42:05       11 阅读
  9. ACE之socket

    2024-04-01 11:42:05       17 阅读
  10. docker 安装Sentinel

    2024-04-01 11:42:05       17 阅读