Last active
September 4, 2020 12:04
-
-
Save Skogsfrae/764824da58ec6df8e400f91a4e3f2a7c to your computer and use it in GitHub Desktop.
Live reload for Qt QML development on go with QQmlApplicationEngine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"os" | |
"github.com/fsnotify/fsnotify" | |
"github.com/therecipe/qt/core" | |
"github.com/therecipe/qt/gui" | |
"github.com/therecipe/qt/qml" | |
) | |
func main() { | |
core.QCoreApplication_SetAttribute(core.Qt__AA_EnableHighDpiScaling, true) | |
gui.NewQGuiApplication(len(os.Args), os.Args) | |
engine := qml.NewQQmlApplicationEngine(nil) | |
engine.Load(core.NewQUrl3("./qml/main.qml", 0)) | |
watcher, err := fsnotify.NewWatcher() | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer watcher.Close() | |
go func() { | |
for { | |
select { | |
case event, ok := <-watcher.Events: | |
if !ok { | |
return | |
} | |
if event.Op&fsnotify.Write == fsnotify.Write { | |
log.Println("modified file:", event.Name) | |
objs := engine.RootObjects() | |
if obj := objs[len(objs)-1]; obj != nil { | |
window := gui.NewQWindowFromPointer(obj.Pointer()) | |
window.DeleteLater() | |
engine.ClearComponentCache() | |
engine.Load(core.NewQUrl3("./qml/main.qml", 0)) | |
} | |
} | |
case err, ok := <-watcher.Errors: | |
if !ok { | |
return | |
} | |
log.Println("error:", err) | |
} | |
} | |
}() | |
err = watcher.Add("./qml") | |
if err != nil { | |
log.Fatal(err) | |
} | |
gui.QGuiApplication_Exec() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment