当前位置:网站首页>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);
}
}
边栏推荐
- regular expression
- Getting started with ARP
- 自定义类型:枚举
- You really should go to the factory to move bricks!
- ArcGIS layer annotation display
- Qt设计机器人仿真控制器——按键控制机器人关节转动
- 荣耀CEO赵明:单一厂商很难实现全场景产品覆盖
- EMQ Yingyun technology was listed on the 2022 "cutting edge 100" list of Chinese entrepreneurs
- Meizu blood exchange: Alibaba quits? Zhuhai SASAC joins the Bureau, and Huang Zhang hands over the controlling stake! Li Nan is removed from the main staff!
- Dongfang Guangyi refuted the rumor late at night, saying that the news that the hammer was filed for investigation was untrue!
猜你喜欢

ArcGIS pixel size changed from 0.00025 to meters

1184. 公交站间的距离

Getting started with ARP

简单使用 MySQL 索引

Summary of experience in using.Net test framework xUnit, mstest, specflow

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

15、ARM嵌入式系统:如何用PC调试单板

自定义类型:枚举

ARP 入门

JVM class loading subsystem
随机推荐
[leetcode] skillfully use bit operation
Intel plans to sell baseband chip business, but Apple has given up?
[machine learning basics] - another perspective to explain SVM
Chapter 2 using API Mgmnt service
Comparison of array and object merging methods assign, merge, defaults, defaultsdeep in lodash
Qt信号和槽连接失败原因及解决办法
Duplicate content in lookup table
我们为什么要推出Getaverse?
EMQ Yingyun technology was listed on the 2022 "cutting edge 100" list of Chinese entrepreneurs
2019q1 global smartphone shipments: Huawei vivo increased significantly, while Apple plummeted 30.2%!
安全的证券公司有哪些?我想在手机上买股票
Envi grid resampling
File browser? QT can also be achieved!
双指针滑动窗口法解析及LeetCode相关题解
Solve the timeliness problem caused by Eureka's default cache configuration
Sword finger offer 25. merge two sorted linked lists
709. Convert to lowercase letters
Getting started with arcpy
Envi5.3 open GF-1 WFV data
PS pull out logo