From 2152ad480e7ca02f542ca0aaeb1dda4cfa49f3c3 Mon Sep 17 00:00:00 2001 From: Rick Calixte <10281587+rcalixte@users.noreply.github.com> Date: Sun, 10 Nov 2024 16:05:35 -0500 Subject: [PATCH] Add goroutine6 Ported from: https://github.com/therecipe/examples/blob/master/advanced/widgets/goroutine/main.go --- .gitignore | 1 + examples/goroutine6/main.go | 52 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 examples/goroutine6/main.go diff --git a/.gitignore b/.gitignore index 8612113f..d7a094e6 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ cmd/genbindings/genbindings cmd/miqt-uic/miqt-uic cmd/miqt-rcc/miqt-rcc +examples/goroutine6/goroutine6 examples/helloworld/helloworld examples/helloworld6/helloworld6 examples/mdoutliner/mdoutliner diff --git a/examples/goroutine6/main.go b/examples/goroutine6/main.go new file mode 100644 index 00000000..1a6a84bb --- /dev/null +++ b/examples/goroutine6/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "os" + "runtime" + "time" + + qt "github.com/mappu/miqt/qt6" +) + +func main() { + runtime.LockOSThread() + qt.NewQApplication(os.Args) + + window := qt.NewQMainWindow2() + window.QWidget.SetFixedSize2(250, 200) + window.QWidget.SetWindowTitle("goroutine Example") + + widget := qt.NewQWidget(window.QWidget) + var layout = qt.NewQVBoxLayout2() + widget.SetLayout(layout.QBoxLayout.QLayout) + window.SetCentralWidget(widget) + + labels := make([]*qt.QLabel, 3) + for i := range labels { + label := qt.NewQLabel(window.QWidget) + label.SetAlignment(qt.AlignCenter) + widget.Layout().AddWidget(label.QWidget) + labels[i] = label + } + + button := qt.NewQPushButton5("start!", window.QWidget) + button.OnClicked1(func(bool) { + button.SetDisabled(true) + for i, label := range labels { + go func(index int, qlabel *qt.QLabel) { + var tick int + for range time.NewTicker(time.Duration((index+1)*25) * time.Millisecond).C { + tick++ + time.Sleep(50 * time.Millisecond) + qlabel.SetText(fmt.Sprintf("%v %v", tick, time.Now().UTC().Format("15:04:05.0000"))) + } + }(i, label) + } + }) + widget.Layout().AddWidget(button.QAbstractButton.QWidget) + + window.Show() + + qt.QApplication_Exec() +}