From fea87aff427b6f683936277a51a8612aa31001f3 Mon Sep 17 00:00:00 2001 From: mappu Date: Mon, 23 Oct 2023 11:00:00 +1300 Subject: [PATCH] initial commit --- binding.cpp | 28 ++++++++++++++++++++ binding.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ binding.h | 25 ++++++++++++++++++ go.mod | 3 +++ main.go | 26 +++++++++++++++++++ 5 files changed, 156 insertions(+) create mode 100644 binding.cpp create mode 100644 binding.go create mode 100644 binding.h create mode 100644 go.mod create mode 100644 main.go diff --git a/binding.cpp b/binding.cpp new file mode 100644 index 00000000..b64a8e73 --- /dev/null +++ b/binding.cpp @@ -0,0 +1,28 @@ +#include "binding.h" +#include +#include +#include + +PQApplication QApplication_new(int argc, char** argv) { + return new QApplication(argc, argv); +} + +PQWidget QWidget_new() { + return new QWidget(); +} + +void QWidget_show(PQWidget self) { + static_cast(self)->show(); +} + +PQPushButton QPushButton_new(const char* label, PQWidget parent) { + return new QPushButton(label, static_cast(parent)); +} + +void QPushButton_show(PQPushButton self) { + static_cast(self)->show(); +} + +int QApplication_exec(PQApplication self) { + return static_cast(self)->exec(); +} diff --git a/binding.go b/binding.go new file mode 100644 index 00000000..2e016b93 --- /dev/null +++ b/binding.go @@ -0,0 +1,74 @@ +package main + +/* + +#cgo pkg-config: Qt5Widgets +#include "binding.h" + +*/ +import "C" + +import ( + "unsafe" +) + +func CArray(data []string) (C.int, **C.char) { + + c_argv := (*[0xfff]*C.char)(C.malloc(C.ulong(8 /* sizeof pointer */ * len(data)))) + + for i, arg := range data { + c_argv[i] = C.CString(arg) + } + + return C.int(len(data)), (**C.char)(unsafe.Pointer(c_argv)) +} + +// + +type QApplication struct { + h C.PQApplication +} + +func NewQApplication(args []string) *QApplication { + h := C.QApplication_new(CArray(args)) + return &QApplication{h: h} +} + +func (this *QApplication) Exec() int { + ret := C.QApplication_exec(this.h) + return int(ret) +} + +// + +type QWidget struct { + h C.PQWidget +} + +func NewQWidget() *QWidget { + ret := C.QWidget_new() + return &QWidget{h: ret} +} + +func (this *QWidget) Show() { + C.QWidget_show(this.h) +} + +// + +type QPushButton struct { + h C.PQPushButton +} + +func NewQPushButton(label string, parent QWidget) *QPushButton { + h := C.QPushButton_new(C.CString(label), parent.h) + return &QPushButton{h: h} +} + +func (this *QPushButton) Show() { + C.QPushButton_show(this.h) +} + +func (this *QPushButton) AsQWidget() *QWidget { + return &QWidget{h: C.PQWidget(this.h)} // Type cast +} diff --git a/binding.h b/binding.h new file mode 100644 index 00000000..a4cfd476 --- /dev/null +++ b/binding.h @@ -0,0 +1,25 @@ +typedef void* PQApplication; + +typedef void* PQPushButton; + +typedef void* PQWidget; + +#ifdef __cplusplus +extern "C" { +#endif + +PQApplication QApplication_new(int argc, char** argv); + +PQWidget QWidget_new(); + +void QWidget_show(PQWidget self); + +PQPushButton QPushButton_new(const char* label, PQWidget parent); + +void QPushButton_show(PQPushButton self); + +int QApplication_exec(PQApplication self); + +#ifdef __cplusplus +} /* extern C */ +#endif diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..1f63e962 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module qtbindingexperiment + +go 1.19 diff --git a/main.go b/main.go new file mode 100644 index 00000000..bcb4889e --- /dev/null +++ b/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "os" +) + +func main() { + + app := NewQApplication(os.Args) + + /* + btn := NewQPushButton("Hello world!", QWidget{}) + fmt.Printf("%#v\n", btn) + + btn.Show() + */ + + wid := NewQWidget() + _ = wid + wid.Show() + + app.Exec() + + fmt.Println("OK!") +}