当前位置:网站首页>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);
}
}
边栏推荐
- 【技术】uniapp 之 在线选座 demo
- OpenMP入门
- Problems encountered in upgrading chrome to version 80 - solutions to system login failure
- Envi grid resampling
- Analysis of double pointer sliding window method and solution of leetcode related problems
- TCP protocol debugging tool tcpengine v1.3.0 tutorial
- Jenkins cli command details
- 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
- Qt信号和槽连接失败原因及解决办法
- Getting started with arcpy
猜你喜欢

Long awaited full platform support - Open Source im project uniapp update of openim

Why should we launch getaverse?

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

Creation and inheritance of JS class

1184. 公交站间的距离

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

剑指 Offer 22. 链表中倒数第k个节点

Simply use MySQL index

EMQ Yingyun technology was listed on the 2022 "cutting edge 100" list of Chinese entrepreneurs

15、ARM嵌入式系统:如何用PC调试单板
随机推荐
Qualcomm reconciled with apple and received at least $4.5 billion in settlement fees! Next goal: Huawei!
Long awaited full platform support - Open Source im project uniapp update of openim
Explain Apache Hudi schema evolution in detail
PS pull out logo
Template method mode
Codeforces round 690 (Div. 3) C. unique number conventional solution
Qt设计机器人仿真控制器——按键控制机器人关节转动
TCP协议调试工具TcpEngine V1.3.0使用教程
ZBar source code analysis - img_ scanner. c | [email protected]
EventLoop event loop mechanism
荣耀CEO赵明:单一厂商很难实现全场景产品覆盖
JVM class loading subsystem
EF combined with sqlbulkcopy batch insert data
Parental delegation mechanism
hping3安装使用
Dongfang Guangyi refuted the rumor late at night, saying that the news that the hammer was filed for investigation was untrue!
AXI协议(1):AMBA总线介绍,AXI概念与背景介绍,AXI协议特点与功能
Why should we launch getaverse?
TCP protocol debugging tool tcpengine v1.3.0 tutorial
JS, call in the for loop is asynchronously converted to synchronous execution