欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 2024-08-01 QML开发小技巧一

2024-08-01 QML开发小技巧一

2024/10/26 7:36:09 来源:https://blog.csdn.net/ihmhm12345/article/details/140852915  浏览:    关键词:2024-08-01 QML开发小技巧一

本集合文章将长期更新一些qml代码中的技巧和问题

正文

有时候可能会遇到同一个qml文件中有多个相同的代码块,如下:

    Column {spacing: 10anchors.leftMargin: 10anchors.left: parent.leftGroupBox {title: "GroupBox"Layout.fillWidth: trueColumn {spacing: 10Button {id: _buttontext: "Open Menu"}TextField {id: _textFieldwidth: 100}Text {id: _texttext: qsTr("text")}}}GroupBox {title: "GroupBox"Layout.fillWidth: trueColumn {spacing: 10Button {// id: _buttontext: "Open Menu"}TextField {// id: _textFieldwidth: 100}Text {// id: _texttext: qsTr("text")}}}}

这样文件内代码量会大量多,且代码在修改时,也不容易定位代码位置。
可能大家都想到重新编写一个qml文件来实现这个GroupBox 里的内容,这确实是可行的,而且很多时候也是这么用的。可是我们可能只是在这个qml文件内使用这个组件,其它qml文件内都不需要使用,这时可以只在这个qml文件内定义一个组件,代码如下:

    component MyGroupBox: GroupBox {title: "GroupBox"Layout.fillWidth: trueproperty alias button: _buttonproperty alias textField: _textFieldproperty alias text: _textColumn {spacing: 10Button {id: _buttontext: "Open Menu"}TextField {id: _textFieldwidth: 100}Text {id: _texttext: qsTr("text")}}}Column {spacing: 10anchors.leftMargin: 10anchors.left: parent.leftMyGroupBox {button.onClicked: console.log("----")textField.text: "Hello"text.text: "Hello"}MyGroupBox {}}

这样不止代码量少,而且可以很方便和编写代码

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com