如何将值推送到QML属性变量二维数组 - 动态?

时间:2022-12-09 21:21:37

This is what I have tried:

这是我尝试过的:

import QtQuick 2.0

Rectangle
{
    property variant twoDimTempArray: [[]]
    property variant oneDArray: [1,2,3]

    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            twoDimTempArray.push (oneDArray)

            twoDimTempArray[0].push (oneDArray)

            twoDimTempArray[0][0] = oneDArray[0]

            console.log (twoDimTempArray)
        }
    }
}

They all results in [].

它们都导致[]。

How to push values in QML property variant two dimensional array?

如何在QML属性变量二维数组中推送值?

1 个解决方案

#1


6  

One way to add the values dynamically to a 1 dimensional QML variant is to fill a normal Javascript array and then assign it to the QML variant.

将值动态添加到1维QML变体的一种方法是填充普通Javascript数组,然后将其分配给QML变体。

import QtQuick 2.0

Rectangle
{
    property variant oneDArray: []
    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            var t = new Array (0)
            t.push(11)
            t.push(12)

            oneDArray = t

            console.log (oneDArray)
        }
    }
}

Output:

Starting /home/.../documents/test/build-junk-Desktop_Qt_5_1_0_GCC_64bit-Debug/junk...
QML debugging is enabled. Only use this in a safe environment.
[11,12]
/home/.../documents/test/build-junk-Desktop_Qt_5_1_0_GCC_64bit-Debug/junk exited with code 0

I have tried the same method for a 2 dimensional array and it works.

我已经尝试了相同的方法的二维数组,它的工作原理。

#1


6  

One way to add the values dynamically to a 1 dimensional QML variant is to fill a normal Javascript array and then assign it to the QML variant.

将值动态添加到1维QML变体的一种方法是填充普通Javascript数组,然后将其分配给QML变体。

import QtQuick 2.0

Rectangle
{
    property variant oneDArray: []
    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            var t = new Array (0)
            t.push(11)
            t.push(12)

            oneDArray = t

            console.log (oneDArray)
        }
    }
}

Output:

Starting /home/.../documents/test/build-junk-Desktop_Qt_5_1_0_GCC_64bit-Debug/junk...
QML debugging is enabled. Only use this in a safe environment.
[11,12]
/home/.../documents/test/build-junk-Desktop_Qt_5_1_0_GCC_64bit-Debug/junk exited with code 0

I have tried the same method for a 2 dimensional array and it works.

我已经尝试了相同的方法的二维数组,它的工作原理。