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 小时前
一、react18+项目初始化(vite)
前端·react.js
ai小鬼头1 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
菜鸟看点1 小时前
自定义Cereal XML输出容器节点
c++·qt
漫步企鹅2 小时前
【蓝牙】Linux Qt4查看已经配对的蓝牙信息
linux·qt·蓝牙·配对
墨菲安全2 小时前
NPM组件 betsson 等窃取主机敏感信息
前端·npm·node.js·软件供应链安全·主机信息窃取·npm组件投毒
GISer_Jing2 小时前
Monorepo+Pnpm+Turborepo
前端·javascript·ecmascript
天涯学馆2 小时前
前端开发也能用 WebAssembly?这些场景超实用!
前端·javascript·面试
new_zhou2 小时前
Windows qt打包编译好的程序
开发语言·windows·qt·打包程序
我在北京coding3 小时前
TypeError: Cannot read properties of undefined (reading ‘queryComponents‘)
前端·javascript·vue.js