How to push values to QML property variant two dimensional array - dynamically?

ⅰ亾dé卋堺 提交于 2019-12-06 03:43:03

问题


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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/26098663/how-to-push-values-to-qml-property-variant-two-dimensional-array-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!