Coding Standard QML
1. QML document structure
Structure 1.1. Basic document and elements structure should be ordered like this:
- Id
- New property declarations
- New signal declarations
- Java Script functions
- Element's property initialization
- Children elements
- States
- Transitions
- Connections
- Others
Rectangle {
id: myButton
property alias text: buttonText.text
signal clicked
function printHello() {
console.log("Hello, from myButton!")
}
color: "red"
height: 100
width: 250
Text {
id: buttonText
anchors.centerIn: parent
}
MouseArea {
id: mouseArea
anchor.fill: parent
onClicked: {
clicked()
}
}
states: [
State {
name: "default"
},
State {
name: "hovered"
}
]
transitions: [
Transition {
from: "default"
to: "hovered"
}
]
Connections {
target: someObject
onSomeObjectAction: console.log("Hello, someObject!");
}
}
Structure 1.2. All above-mentioned groups should be separated with empty line.
Structure 1.3. Put properties and functions for internal usage into QtObject to avoid changing outside.
Rectangle {
id: myRect
QtObject {
id: internal
property string someInternalText: "Hello"
}
}
Structure 1.4. All "magic" numbers and text names should be moved to "readonly" properties
Rectangle {
readonly property int meaningOfLife: 42
readonly property string stateName: "stateName"
}
2. Groped properties
Groped 2.1. Use group notation for group properties instead of dot notation.
Rectangle {
anchor.top: parent.top
anchor.topMargin: 42
anchor.left: parent.left
}
Text {
text: "Some Text"
font.pointSize: 18
font.bold: true
}
Rectangle {
anchor {
top: parent.top
topMargin: 42
left: parent.left
}
}
Text {
text: "Some Text"
font{
pointSize: 18
bold: true
}
}
3.1 Naming convention
Naming 3.1.1. Use camel case style for properties, java script functions, java script variables, signals, ids.
Naming 3.1.2. All names should start with lower case letter.
Naming 3.1.3. All names should have logical meaning. Avoid abbreviation and single letter names.
3.2 Brackets style
Brackets 3.2.1 Open bracket should be in the same line as expression or object name
Rectangle
{
}
if (isTrue)
{
}
else
{
}
function myFunction (parameter)
{
}
Rectangle {
}
if (isTrue) {
} else {
}
function myFunction (parameter) {
}
Brackets 3.2.2 Do not use curly braces, if there is single or simple expression
function printText() {
console.log("Hello")
}
MouseArea {
onClicked : {
printText()
}
}
MouseArea {
onClicked : printText()
}
3.3 Semicolons
Semicolons 3.3.1 Avoid to use semicolons in QML code where it is possible.
function myFunction() {
var firstVar = 1
var secondVar = 2
return firstVar + secondVar
}
4. QML Files naming
Files 4.1. All QML files should be name in camel case style and name should begin with capital letter.
5.Best Practices and Advice
Advice 5.1. Try to avoid heavy calculation and data manipulation in QML/Java Script code. Use QML for UI purpose. For heavy operation use C++.