簡體   English   中英

如何一次將 3 或 4 個變量推入 QML 中的數組列表

[英]How can I push 3 or 4 variables into the array list in QML at a time

我是 qml 和 javascript 技術的新手。 我需要有關陣列上的推送和彈出操作的示例。 例如,如何一次將 3 個變量推入 QML 中的數組列表。 我希望 output 如下所示 [{"A001", 1, "Item1"}, {"A002", 2, "Item2"}]。 我從示例代碼開始,但無法完成 Component.onCompleted 中的邏輯。

Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")

property var object: ({})
property var list: []

property string str1: "A001"
property int value1: 1
property string item1: "Item1"

property string str2: "A002"
property int value2: 2
property string item2: "Item2"

Component.onCompleted: {
    //1. Frame the object {str1, value1, item1}
    //2. Push to the array using push operation
    //3. The output of the array to be [{"A001", 1, "Item1"}]

    //4. Frame the object {str2, value2, item2}
    //5. Push to the array using push operation
    //6. The output of the array to be [{"A001", 1, "Item1"},{"A002", 2, "Item2"}]
    
    //7. Do pop operation on the array
    //8. The output of the array to be [{"A001", 1, "Item1"}]

    //9. Do pop operation on the array
    //10. The output of the array to be []
}

}

看起來你想要一個對象數組。 在 javascript 中,對象是鍵/值對。 所以你必須像這樣創建它們:

var obj1 = {"str": str1, "value": value1, "item": item1}
var obj2 = {"str": str2, "value": value2, "item": item2}

然后你可以將它們推入或彈出到你的數組中:

var arr = [];
arr.push(obj1);
arr.push(obj2);
arr.pop();
arr.pop();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM