something

  • 字符串去除前后空格:str.trim();
  • 获取当前时间:Qt.formatFromData(new Date(), "yyyy-MM-dd:ss:mm:zzz");
  • 只读property: readonly property var name: 1;
  • text自动换行参数: wrapMode: Text.Wrap

ListModel

  1. 申明
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    property ListModel data_model: ListModel{
    ListElement {
    custom_text: "a"
    enable: false
    }
    ListElement {
    custom_text: "b"
    enable: false
    }
    }
  2. 获取长度: data_model.count
  3. 追加:
    1
    2
    3
    var data = {"custom_text": "c",
    "enable": true}
    data_model.append(data)
  4. 指定位置插入: data_model.insert(0, data)
  5. 获取索引0指定位置的model: data_model.get(0)
  6. 修改指定位置元素:data_model.setProperty(0, "custom_text", "z")

生成随机字母数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function randomRange(min, max) {
return Math.round(Math.random() * (max - min) + min)
}
function randomStr(len) {
// 随机字符串
var rand_str = ""
for (var i = 0; i < len; ++i) {
var num = randomRange(0, 9)
var lower = String.fromCharCode(randomRange(97, 122))
var upper = String.fromCharCode(randomRange(65, 90))
rand_str = rand_str + num + lower + upper
}
// 随机字符串中随机取字符
var result = ""
for (var i = 0; i < len; ++i) {
result += rand_str[randomRange(0, rand_str.length - 1)];
}
return result
}

// example
randomStr(4) // 生成长度为4的数字字母随机字符串

父控件适应子控件大小

使用childrenRect属性childrenRect:

childrenRect.height : real
childrenRect.width : real
childrenRect.x : real
childrenRect.y : real

比如:

1
2
3
4
5
6
7
8
9
Rectangle {
width: childrenRect.width
height: childrenRect.height
color: "red"

Label {
text: "child label"
}
}