[GH-ISSUE #151] Set multi-dimensional int Arrays from inside .NET #93

Closed
opened 2026-05-05 11:04:22 -06:00 by gitea-mirror · 1 comment
Owner

Originally created by @geigertom on GitHub (Jul 4, 2019).
Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/151

I want to draw shapes on QML Canvas elements that I calculate in .NET.

In QML, I can do the following:

Canvas {
        id: canvas
        anchors.fill: parent;
        property variant positions: [[10,10],[200,10],[200,200],[10,200]];

        onPaint:{
                var ctx = canvas.getContext('2d');
                ctx.reset()
                ctx.strokeStyle = "blue";
                ctx.lineWidth = 10;
                ctx.beginPath();
                ctx.moveTo(positions[0][0], positions[0][1]);
                for (var i=1; i<positions.length; i++) {
                        ctx.lineTo(positions[i][0], positions[i][1])
                }
                ctx.closePath();
                ctx.stroke();
        }
}

I would like to be able to set the multi-dimensional array property var positions from inside .NET. What would be the best approach? Do we need to extend NetVariant for another type, or is this already implemented?

Thanks
Tom

Originally created by @geigertom on GitHub (Jul 4, 2019). Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/151 I want to draw shapes on QML `Canvas` elements that I calculate in .NET. In QML, I can do the following: ``` Canvas { id: canvas anchors.fill: parent; property variant positions: [[10,10],[200,10],[200,200],[10,200]]; onPaint:{ var ctx = canvas.getContext('2d'); ctx.reset() ctx.strokeStyle = "blue"; ctx.lineWidth = 10; ctx.beginPath(); ctx.moveTo(positions[0][0], positions[0][1]); for (var i=1; i<positions.length; i++) { ctx.lineTo(positions[i][0], positions[i][1]) } ctx.closePath(); ctx.stroke(); } } ``` I would like to be able to set the multi-dimensional array `property var positions` from inside .NET. What would be the best approach? Do we need to extend `NetVariant` for another type, or is this already implemented? Thanks Tom
Author
Owner

@geigertom commented on GitHub (Jul 5, 2019):

I did some testing and debugging and as it turns out, this already works:

.NET property:

[NotifySignal("intArrayPropertyChanged")]
public int[,] IntArrayProperty { get; set; } = new int [,] {{1,2,3},{3,4,3}};

QML:

var dimensions = 3;
for (var i=0; i<test.intArrayProperty.length / dimensions; i++) {
    console.log(test.intArrayProperty.get(i,0));
    console.log(test.intArrayProperty.get(i,1));
    console.log(test.intArrayProperty.get(i,2));
}

You need to know the number of dimensions for iterating beforehand.

<!-- gh-comment-id:508858621 --> @geigertom commented on GitHub (Jul 5, 2019): I did some testing and debugging and as it turns out, this already works: .NET property: ``` [NotifySignal("intArrayPropertyChanged")] public int[,] IntArrayProperty { get; set; } = new int [,] {{1,2,3},{3,4,3}}; ``` QML: ``` var dimensions = 3; for (var i=0; i<test.intArrayProperty.length / dimensions; i++) { console.log(test.intArrayProperty.get(i,0)); console.log(test.intArrayProperty.get(i,1)); console.log(test.intArrayProperty.get(i,2)); } ``` You need to know the number of dimensions for iterating beforehand.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/qmlnet#93
No description provided.