Skip to content

Commit

Permalink
Document using QML.jl from a Julia module
Browse files Browse the repository at this point in the history
Issue Properties passed to `loadqml` makes app to crash when loaded from module #165
Issue Segfault when calling Julia functions #183
  • Loading branch information
barche committed Oct 16, 2023
1 parent 0b987e2 commit d822b16
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,64 @@ Most fundamental types are converted implicitly. Mind that the default integer t

We also convert `QVariantMap`, exposing the indexing operator `[]` to access element by a string key. This mostly to deal with arguments passed to the QML `append` function in list models.

## Using QML.jl inside another Julia module

Because of precompilation and because the QML engine gets destroyed between runs, care needs to be taken to avoid storing invalid pointers and to call all `@qmlfunction` macros again before restarting the program. An example module that takes care of this could look like this:

```julia
module QmlModuleTest

using QML
# absolute path in case working dir is overridden
const qml_file = joinpath(dirname(@__FILE__), "qml", "frommodule.qml")

# Propertymap is a pointer, so it needs to be a function and not a const value
props() = JuliaPropertyMap(
"hi" => "Hi",
)

world() = " world!"

# Convenience function to call all relevant function macros
function define_funcs()
@qmlfunction world
end

function main()
define_funcs()
loadqml(qml_file; props=props())
exec()
end

end # module QmlModuleTest
```

The referred `frommodule.qml` file is expected in a subdirectory called `qml`, with the following content:

```qml
import QtQuick
import QtQuick.Controls
import org.julialang
ApplicationWindow {
id: mainWin
title: "My Application"
width: 100
height: 100
visible: true
Rectangle {
anchors.fill: parent
color: "red"
Text {
anchors.centerIn: parent
text: props.hi + Julia.world()
}
}
}
```

## Interface

```@index
Expand Down

0 comments on commit d822b16

Please sign in to comment.