mirror of
https://github.com/mappu/miqt.git
synced 2024-12-22 00:48:38 +00:00
handbindings: explore how to bind connect() calls
This commit is contained in:
parent
6928a87b4b
commit
e57459c5e7
6
.gitignore
vendored
6
.gitignore
vendored
@ -3,9 +3,9 @@
|
||||
gen_*
|
||||
|
||||
# binaries
|
||||
miqt
|
||||
cmd/bindings_test/direct
|
||||
cmd/bindings_test/testapp
|
||||
cmd/handbindings/handbindings
|
||||
cmd/handbindings/bindings_test/direct
|
||||
cmd/handbindings/bindings_test/testapp
|
||||
cmd/genbindings/genbindings
|
||||
|
||||
|
||||
|
@ -3,6 +3,10 @@
|
||||
#include <QWidget>
|
||||
#include <QPushButton>
|
||||
|
||||
extern "C" {
|
||||
extern void miqt_exec_callback(void* cb, int argc, void* argv);
|
||||
}
|
||||
|
||||
PQApplication QApplication_new(int* argc, char** argv) {
|
||||
// QApplication takes these parameters byref, not by value
|
||||
return new QApplication(*argc, argv);
|
||||
@ -24,6 +28,12 @@ void QPushButton_show(PQPushButton self) {
|
||||
static_cast<QPushButton*>(self)->show();
|
||||
}
|
||||
|
||||
void QPushButton_connect_pressed(PQPushButton self, void* cb) {
|
||||
QPushButton::connect(static_cast<QPushButton*>(self), &QPushButton::pressed, [=]() {
|
||||
miqt_exec_callback(cb, 0, nullptr);
|
||||
});
|
||||
}
|
||||
|
||||
int QApplication_exec(PQApplication self) {
|
||||
return static_cast<QApplication*>(self)->exec();
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ package main
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"runtime/cgo"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@ -24,6 +25,21 @@ func CArray(data []string) (C.int, **C.char) {
|
||||
return C.int(len(data)), (**C.char)(unsafe.Pointer(c_argv))
|
||||
}
|
||||
|
||||
type CallbackFunc func(argc C.int, args *C.void)
|
||||
|
||||
//export miqt_exec_callback
|
||||
func miqt_exec_callback(cb *C.void, argc C.int, args *C.void) {
|
||||
// Our CABI for all callbacks is void(int, void*).
|
||||
// Our Go ABI is CallbackFunc
|
||||
// Then the Go bindings can unmarshal the arguments and C.free() them as necessary
|
||||
cfunc, ok := (cgo.Handle(uintptr(unsafe.Pointer(cb))).Value()).(CallbackFunc)
|
||||
if !ok {
|
||||
panic("miqt: callback of non-callback type (heap corruption?)")
|
||||
}
|
||||
|
||||
cfunc(argc, args)
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
type QApplication struct {
|
||||
@ -81,3 +97,14 @@ func (this *QPushButton) Show() {
|
||||
func (this *QPushButton) AsQWidget() *QWidget {
|
||||
return &QWidget{h: C.PQWidget(this.h)} // Type cast
|
||||
}
|
||||
|
||||
func (this *QPushButton) OnPressed(cb func()) {
|
||||
var cbWrapper CallbackFunc = func(argc C.int, args *C.void) {
|
||||
// Unmarshal arguments
|
||||
// Pressed() doesn't take any, though
|
||||
cb()
|
||||
}
|
||||
|
||||
C.QPushButton_connect_pressed(this.h, unsafe.Pointer(uintptr(cgo.NewHandle(cbWrapper))))
|
||||
// TODO allow disconnect'ing, or tie lifespan for handle.Delete(), ...
|
||||
}
|
||||
|
@ -5,6 +5,8 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef void* PQApplication;
|
||||
|
||||
typedef void* PQPushButton;
|
||||
@ -21,6 +23,8 @@ PQPushButton QPushButton_new(const char* label, PQWidget parent);
|
||||
|
||||
void QPushButton_show(PQPushButton self);
|
||||
|
||||
void QPushButton_connect_pressed(PQPushButton self, void* cb);
|
||||
|
||||
int QApplication_exec(PQApplication self);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -23,6 +23,10 @@ func main() {
|
||||
btn := NewQPushButton("Hello world!", wid)
|
||||
_ = btn
|
||||
|
||||
btn.OnPressed(func() {
|
||||
fmt.Println("Clicked!")
|
||||
})
|
||||
|
||||
wid.Show()
|
||||
|
||||
app.Exec()
|
||||
|
Loading…
Reference in New Issue
Block a user