Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass variable list of coordinates from Nim to QML? #40

Open
arkanoid87 opened this issue Jan 10, 2022 · 2 comments
Open

How to pass variable list of coordinates from Nim to QML? #40

arkanoid87 opened this issue Jan 10, 2022 · 2 comments

Comments

@arkanoid87
Copy link

arkanoid87 commented Jan 10, 2022

I've a list of polygons described like

type
  Point = seq[float]
  Polygon = seq[Point]
  Model = seq[Polygon]

let data = @[
  @[
    @[9.183454513549805,45.464374726885],
    @[9.188776016235352,45.46058215193312],
    @[9.190363883972166,45.469039846107734],
    @[9.183454513549805,45.464374726885]
  ]
]

I'm trying to write QAbstractListModel for MapItemView to display those polygons, but the delegate requires path to be an array of javascript objects

Map {
    MapPolygon {
        color: 'green'
        path: [
            { latitude: -27, longitude: 153.0 },
            { latitude: -27, longitude: 154.1 },
            { latitude: -28, longitude: 153.5 }
        ]
    }
}

I'm trying to understand how to pass a seq of coordinates from my data proc considering that each polygon may have a variable number of vertices

method data(self: MapModel, index: QModelIndex, role: int): QVariant = ...

I see that nimqml lacks QVariantList and QVariantMap but I am not aware if a workaround solution is possible.

I'm considering turning Polygon type into a QObject and make seq[Point] one of its properties, but I don't know if it's possible to pass sequences from/to nimqml, that would mean declaring a list property using QQmlListProperty but it seems unavailable

@arkanoid87
Copy link
Author

I see that dotherside already knows about QVariantList

maybe it could be the shortest path to add this feature

@arkanoid87
Copy link
Author

I've successfully passed a list from nim to qml by adding this to dotherside.nim

proc dos_qvariant_setArray(variant: DosQVariant, size: cint, array: ptr DosQVariantArray) {.cdecl, dynlib: dynLibName, importc.}

and this to qvariant.nim

proc toQVariant*(sequence: seq[QVariant]): QVariant =
  result = newQVariant()
  var arr = newSeqOfCap[DosQVariant](sequence.len)
  for e in sequence:
    arr.add e.vptr
  dos_qvariant_setArray(result.vptr, sequence.len.cint, cast[ptr DosQVariantArray](arr[0].unsafeAddr))
  return result

returning the generated QVariant to qml via data method of model it ended up correctly on the other side

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant