Replies: 6 comments 7 replies
-
See issue #7 Purego is not magic, it only allows dlsym/LoadLibrary of some C ABI library. But Qt is a C++ library, and the C++ ABI is compiler-defined, and function names are mangled. So, this requires either
|
Beta Was this translation helpful? Give feedback.
-
package main
import (
"github.com/ebitengine/purego"
"golang.org/x/sys/windows"
"runtime"
"syscall"
"unsafe"
)
func main() {
runtime.LockOSThread()
a := NewApp(0, []string{"", ""})
qWidget := NewQWidget()
b := NewQPushButton("button", qWidget)
b.OnPressed(func() {
})
b.Show()
qWidget.Show()
a.Exec()
}
var (
QApplication_newProc *syscall.Proc
QWidget_newProc *syscall.Proc
QWidget_showProc *syscall.Proc
QPushButton_newProc *syscall.Proc
QPushButton_showProc *syscall.Proc
QPushButton_connect_pressedProc *syscall.Proc
QApplication_execProc *syscall.Proc
miqt_exec_callbackProc *syscall.Proc
)
func init() {
check(windows.SetDllDirectory("."))
dll := syscall.MustLoadDLL("purego.dll")
QApplication_newProc = dll.MustFindProc("QApplication_new")
QWidget_newProc = dll.MustFindProc("QWidget_new")
QWidget_showProc = dll.MustFindProc("QWidget_show")
QPushButton_newProc = dll.MustFindProc("QPushButton_new")
QPushButton_showProc = dll.MustFindProc("QPushButton_show")
QPushButton_connect_pressedProc = dll.MustFindProc("QPushButton_connect_pressed")
QApplication_execProc = dll.MustFindProc("QApplication_exec")
miqt_exec_callbackProc = dll.MustFindProc("miqt_exec_callback")
}
type App struct {
handle uintptr //todo add more fields from C++ class members
}
func NewApp(argc int, argv []string) *App {
// 设置参数数量
argcPtr := uintptr(unsafe.Pointer(&argc))
// 为argv创建指针。如果没有参数,可以设置为nil
var argvPtr uintptr
if argv != nil && len(argv) > 0 {
argvPtr = uintptr(unsafe.Pointer(&argv[0])) // 获取argv的指针
} else {
argvPtr = 0 // 或者使用nil
}
// 调用外部函数
r1, _, err := QApplication_newProc.Call(argcPtr, argvPtr)
check(err)
return &App{
handle: r1,
}
}
func (a *App) Exec() {
QApplication_execProc.Call(a.handle)
}
func check(err error) {
if err != nil {
//panic(err)
}
}
type QWidget struct {
handle uintptr //todo add more fields from C++ class members
}
func NewQWidget() *QWidget {
r1, _, err := QWidget_newProc.Call()
check(err)
return &QWidget{
handle: r1,
}
}
func (w *QWidget) Show() {
QWidget_showProc.Call(w.handle)
}
type QPushButton struct {
handle uintptr //todo add more fields from C++ class members
}
func NewQPushButton(text string, parent *QWidget) *QPushButton {
title := [256]byte{}
copy(title[:], []byte(text))
r1, _, err := QPushButton_newProc.Call(
uintptr(unsafe.Pointer(&title)),
uintptr(unsafe.Pointer(parent.handle)),
)
check(err)
return &QPushButton{
handle: r1,
}
}
func (b *QPushButton) Show() {
QPushButton_showProc.Call(b.handle)
}
func (b *QPushButton) OnPressed(callback func()) {
return
purego.NewCallback(callback) //todo
QPushButton_connect_pressedProc.Call(b.handle)
} |
Beta Was this translation helpful? Give feedback.
-
Hi, still can't debug it after testing, also, not sure if it's possible to load so on android. do you have a static version of qt6.8 compiled by vs2022 on your side? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
i forked a rep for make static qt 6.9 use action The next step is to add static lib compilation for android, I don't know how to implement this inside action. The last step is to copy miqt's c abi bindings to generate code to implement dynamic libraries in pre-compiled c abi format for all platforms inside action. |
Beta Was this translation helpful? Give feedback.
-
Hello, I have a question for you. All the testing I've done so far has been under Windows, is there a way to use parse cmake or something else instead of pkg config to parse the compilation parameters, I'm guessing it should parse out the header file directory, compilation parameters and lib directory which are the configurations passed to clang to dump ast. I'll try copying the .pc file of the qt lib under Linux first. For now I need to get bindgen running before I can further change the header file export api operation. If you have time to spare, please check every one of my commits, thanks. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
use https://github.com/ebitengine/purego
Beta Was this translation helpful? Give feedback.
All reactions