当前位置:网站首页>QML coding specification
QML coding specification
2022-07-24 16:42:00 【Liu-Eleven】
Catalog
QML Object declaration
QML Object properties are generally constructed in the following order :
- id
- Property declaration
- Signal declaration
- JavaScript function
- Object properties
- Sub object
- state
- State switching
For better readability , It is recommended to add a blank line between different parts . for example , Let's use a Photo Object as an example :
Rectangle {
id: photo // id On the first line , Easy to find an object
property bool thumbnail: false // Property declaration
property alias image: photoImage.source
signal clicked // Signal declaration
function doSomething(x) // javascript function
{
return x + photoImage.width
}
color: "gray" // Object properties
x: 20; y: 20; height: 150 // Put related attributes together
width: { // binding
if (photoImage.width > 200) {
photoImage.width;
} else {
200;
}
}
Rectangle { // Sub object
id: border
anchors.centerIn: parent; color: "white"
Image { id: photoImage; anchors.centerIn: parent }
}
states: State { // state
name: "selected"
PropertyChanges { target: border; color: "red" }
}
transitions: Transition { // transition
from: ""; to: "selected"
ColorAnimation { target: border; duration: 200 }
}
}Attribute group
If you use more than one of a set of attributes , Then use group notation , Instead of using point notation , This can improve readability . for example :
Rectangle {
anchors.left: parent.left; anchors.top: parent.top;
anchors.right: parent.right; anchors.leftMargin: 20
}
Text {
text: "hello"
font.bold: true; font.italic: true; font.pixelSize: 20;
font.capitalization: Font.AllUppercase
}It can be written like this :
Rectangle {
anchors { left: parent.left; top: parent.top; right: parent.right; leftMargin: 20 }
}
Text {
text: "hello"
font { bold: true; italic: true; pixelSize: 20; capitalization: Font.AllUppercase }
}list
If a list contains only one element , So we usually ignore square brackets . For example, the following code :
states: [
State {
name: "open"
PropertyChanges { target: container; width: 200 }
}
]It can be written. :
states: State {
name: "open"
PropertyChanges { target: container; width: 200 }
}JavaScript Code
If the script is a separate expression , Recommended direct use :
Rectangle { color: "blue"; width: parent.width / 3 }If the script has only a few lines , Then it is suggested to write it in a piece :
Rectangle {
color: "blue"
width: {
var w = parent.width / 3
console.debug(w)
return w
}
}If the script has many lines , Or need to be used by different objects , Then it is recommended to create a function , Then call it like this :
function calculateWidth(object)
{
var w = object.width / 3
// ...
// more javascript code
// ...
console.debug(w)
return w
}
Rectangle { color: "blue"; width: calculateWidth(parent) }If it's a long script , We can put this function in a separate JavaScript In file , Then import it like this :
import "myscript.js" as Script
Rectangle { color: "blue"; width: Script.calculateWidth(parent) }If the code exceeds one line , So in the block , We use semicolons to indicate the end of each statement :
MouseArea {
anchors.fill: parent
onClicked: {
var scenePos = mapToItem(null, mouseX, mouseY);
console.log("MouseArea was clicked at scene pos " + scenePos);
}
}
边栏推荐
- 解决Eureka默认缓存配置导致时效性问题
- AXI协议(3):AXI架构的握手机制和实现细节
- 详解 Apache Hudi Schema Evolution(模式演进)
- Codeforces round 690 (Div. 3) B. last year's substring conventional solution
- 2019q1 global smartphone shipments: Huawei vivo increased significantly, while Apple plummeted 30.2%!
- Problems encountered in upgrading chrome to version 80 - solutions to system login failure
- Qt信号和槽连接失败原因及解决办法
- What does Baidu promote "delete and recall" mean?
- js,for循环内callback异步转换成同步执行
- 1163: 松哥的分数拆分(C语言)
猜你喜欢

会议OA项目进度(二)

Custom types: Enumeration

1184. 公交站间的距离

Duplicate content in lookup table

JVM class loading subsystem

Servlet framework (servlet+jsp) + addition, deletion, modification and query + paging implemented by MySQL (function package student information entry, addition, deletion, modification and query of st

Simply use MySQL index

MODIS data WGet Download

Comparison of array and object merging methods assign, merge, defaults, defaultsdeep in lodash

ArcGIS layer annotation display
随机推荐
EMQ Yingyun technology was listed on the 2022 "cutting edge 100" list of Chinese entrepreneurs
[LeetCode]38.报数——题解(执行用时击败91% ,内存消耗击败 97%)
如何在 PHP 中防止 XSS
Sword finger offer 22. the penultimate node in the linked list
1184. 公交站间的距离
我们为什么要推出Getaverse?
Servlet framework (servlet+jsp) + addition, deletion, modification and query + paging implemented by MySQL (function package student information entry, addition, deletion, modification and query of st
Duplicate content in lookup table
JVM class loading subsystem
The differences of several deletions in SQL
Dongfang Guangyi refuted the rumor late at night, saying that the news that the hammer was filed for investigation was untrue!
会议OA项目进度(一)
简易版QQ?Qt也可以实现!(一)
IP的概念,IP的分类,IP复用技术
Unity 相机自由移动控制
2019q1 global smartphone shipments: Huawei vivo increased significantly, while Apple plummeted 30.2%!
QT design robot simulation controller -- key control robot joint rotation
期盼已久全平台支持-开源IM项目OpenIM之uniapp更新
ArcGIS create vector
Jenkins CLI 命令详解