initial commit

This commit is contained in:
mappu 2023-10-23 11:00:00 +13:00
commit fea87aff42
5 changed files with 156 additions and 0 deletions

28
binding.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "binding.h"
#include <QApplication>
#include <QWidget>
#include <QPushButton>
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<QWidget*>(self)->show();
}
PQPushButton QPushButton_new(const char* label, PQWidget parent) {
return new QPushButton(label, static_cast<QWidget*>(parent));
}
void QPushButton_show(PQPushButton self) {
static_cast<QPushButton*>(self)->show();
}
int QApplication_exec(PQApplication self) {
return static_cast<QApplication*>(self)->exec();
}

74
binding.go Normal file
View File

@ -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
}

25
binding.h Normal file
View File

@ -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

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module qtbindingexperiment
go 1.19

26
main.go Normal file
View File

@ -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!")
}