Merge pull request #83 from mappu/miqt-lockosthread

Call runtime.LockOSThread() automatically for the Qt main thread
This commit is contained in:
mappu 2024-11-19 17:48:34 +13:00 committed by GitHub
commit 47d4581628
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 44 additions and 0 deletions

View File

@ -89,6 +89,8 @@ Qt class inherited types are projected as a Go embedded struct. For example, to
- When a Qt subclass adds a method overload (e.g. `QMenu::addAction(QString)` vs `QWidget::addAction(QAction*)`), the base class version is shadowed and can only be called via `myQMenu.QWidget.AddAction(QAction*)`.
The Go runtime migrates goroutines between OS threads, but Qt expects fixed OS threads to be used for each QObject. When you first call `qt.NewQApplication` in MIQT, that will be considered the [Qt main thread](https://doc.qt.io/qt-6/thread-basics.html#gui-thread-and-worker-thread) and will automatically signal the Go runtime to bind to a fixed OS thread using `runtime.LockOSThread()`.
Some C++ idioms that were difficult to project were omitted from the binding. But, this can be improved in the future.
### Q6. Can I use Qt Designer and the Qt Resource system?

View File

@ -272,6 +272,12 @@ func (gfs *goFileState) emitParametersGo2CABIForwarding(m CppMethod) (preamble s
tmp = append(tmp, "argc, &argv[0]")
// Additional quirk for QApplication constructor: bind to OS thread
gfs.imports["runtime"] = struct{}{}
preamble += "\n"
preamble += "runtime.LockOSThread() // Prevent Go from migrating the main Qt thread\n"
preamble += "\n"
} else if skipNext {
// Skip this parameter, already handled
skipNext = false

View File

@ -61,6 +61,9 @@ func NewQApplication(args []string) *QApplication {
for i := range args {
argv[i] = C.CString(args[i])
}
runtime.LockOSThread() // Prevent Go from migrating the main Qt thread
ret := C.QApplication_new(argc, &argv[0])
return newQApplication(ret)
}
@ -74,6 +77,9 @@ func NewQApplication2(args []string, param3 int) *QApplication {
for i := range args {
argv[i] = C.CString(args[i])
}
runtime.LockOSThread() // Prevent Go from migrating the main Qt thread
ret := C.QApplication_new2(argc, &argv[0], (C.int)(param3))
return newQApplication(ret)
}

View File

@ -59,6 +59,9 @@ func NewQCoreApplication(args []string) *QCoreApplication {
for i := range args {
argv[i] = C.CString(args[i])
}
runtime.LockOSThread() // Prevent Go from migrating the main Qt thread
ret := C.QCoreApplication_new(argc, &argv[0])
return newQCoreApplication(ret)
}
@ -72,6 +75,9 @@ func NewQCoreApplication2(args []string, param3 int) *QCoreApplication {
for i := range args {
argv[i] = C.CString(args[i])
}
runtime.LockOSThread() // Prevent Go from migrating the main Qt thread
ret := C.QCoreApplication_new2(argc, &argv[0], (C.int)(param3))
return newQCoreApplication(ret)
}

View File

@ -53,6 +53,9 @@ func NewQGuiApplication(args []string) *QGuiApplication {
for i := range args {
argv[i] = C.CString(args[i])
}
runtime.LockOSThread() // Prevent Go from migrating the main Qt thread
ret := C.QGuiApplication_new(argc, &argv[0])
return newQGuiApplication(ret)
}
@ -66,6 +69,9 @@ func NewQGuiApplication2(args []string, param3 int) *QGuiApplication {
for i := range args {
argv[i] = C.CString(args[i])
}
runtime.LockOSThread() // Prevent Go from migrating the main Qt thread
ret := C.QGuiApplication_new2(argc, &argv[0], (C.int)(param3))
return newQGuiApplication(ret)
}

View File

@ -53,6 +53,9 @@ func NewQApplication(args []string) *QApplication {
for i := range args {
argv[i] = C.CString(args[i])
}
runtime.LockOSThread() // Prevent Go from migrating the main Qt thread
ret := C.QApplication_new(argc, &argv[0])
return newQApplication(ret)
}
@ -66,6 +69,9 @@ func NewQApplication2(args []string, param3 int) *QApplication {
for i := range args {
argv[i] = C.CString(args[i])
}
runtime.LockOSThread() // Prevent Go from migrating the main Qt thread
ret := C.QApplication_new2(argc, &argv[0], (C.int)(param3))
return newQApplication(ret)
}

View File

@ -59,6 +59,9 @@ func NewQCoreApplication(args []string) *QCoreApplication {
for i := range args {
argv[i] = C.CString(args[i])
}
runtime.LockOSThread() // Prevent Go from migrating the main Qt thread
ret := C.QCoreApplication_new(argc, &argv[0])
return newQCoreApplication(ret)
}
@ -72,6 +75,9 @@ func NewQCoreApplication2(args []string, param3 int) *QCoreApplication {
for i := range args {
argv[i] = C.CString(args[i])
}
runtime.LockOSThread() // Prevent Go from migrating the main Qt thread
ret := C.QCoreApplication_new2(argc, &argv[0], (C.int)(param3))
return newQCoreApplication(ret)
}

View File

@ -53,6 +53,9 @@ func NewQGuiApplication(args []string) *QGuiApplication {
for i := range args {
argv[i] = C.CString(args[i])
}
runtime.LockOSThread() // Prevent Go from migrating the main Qt thread
ret := C.QGuiApplication_new(argc, &argv[0])
return newQGuiApplication(ret)
}
@ -66,6 +69,9 @@ func NewQGuiApplication2(args []string, param3 int) *QGuiApplication {
for i := range args {
argv[i] = C.CString(args[i])
}
runtime.LockOSThread() // Prevent Go from migrating the main Qt thread
ret := C.QGuiApplication_new2(argc, &argv[0], (C.int)(param3))
return newQGuiApplication(ret)
}