QML文件之间的参数互相调用问题

本文介绍如何在两个QML文件(A.qml和B.qml)之间进行参数调用。通过将A.qml作为B.qml的一个组件,使得A.qml能够访问B.qml中的全局变量,如buttonB1的宽度等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不同QML文件之间的参数互相调用

假设有两个qml文件,分别为A.qml和B.qml,其中A.qml代码实现为:

Rectangle {
    id:aRec;
    width:640;
    height:480;

    Button {
        id:buttonA1;
        width:30;
        height:20;
    }
    Button {
        id:buttonA2;
        width:30;
        height:20;
    }
}

B.qml的代码实现为:

Rectangle {
    id:bRec;
    width:640;
    height:480;

    Button {
        id:buttonB1;
        width:30;
        height:20;
    }
    Button {
        id:buttonB2;
        width:30;
        height:20;
    }
}

如果想要在A.qml中调用B.qml中的buttonB1的属性值,需要作如下修改:

Rectangle {
    id:bRec;
    width:640;
    height:480;

    Button {
        id:buttonB1;
        width:30;
        height:20;
    }
    Button {
        id:buttonB2;
        width:30;
        height:20;
    }
    A {
        id:a
    }
}

解释:B.qml修改之后,A相当于B的子控件,子控件可以调用父控件的所有全局变量值,即在A.qml中可以调用到例如buttonB1.width等属性。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值