Using EventFilters #204
-
I noticed that some functions expect to inherit type Filter struct {
qt6.QObject
}
func (f *Filter) EventFilter(obj *qt6.QObject, event *qt6.QEvent) bool {
slog.Info("EventFilter", "event", event.Type(), "object", obj.ObjectName())
return false
} then application.InstallEventFilter(&new(Filter).QObject) tried to add the filter to the window itself I also didn't get any logs. tried to add it to the widget I'm trying to intercept its events (WebEngineView). also didn't print logs. So either I'm missing something related to Qt in general or to how the binding works. any pointers will be apprechiated |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I'm working on a precise example that should be of help (see below) but the long and short of it is that my understanding is that you would create a new QObject type in addition to your other object (let's say a QLabel in this instance), no need for a struct: o := qt.NewQObject() You would then use l := qt.NewQLabel2()
o := qt.NewQObject()
o.OnEventFilter(func(super func(watched *qt.QObject, event *qt.QEvent) bool, watched *qt.QObject, event *qt.QEvent) bool {
// do your things here
}
l.InstallEventFilter(o) I'm basing this on my reading of the documentation for EDIT: It's a little sloppy but this works for me: package main
import (
"fmt"
"os"
"strconv"
qt "github.com/mappu/miqt/qt6"
)
func main() {
qt.NewQApplication(os.Args)
w := qt.NewQWidget2()
w.SetFixedWidth(400)
w.SetFixedHeight(100)
l := qt.NewQLabel5("Press any of the keys on your keyboard!", w)
l.SetFocusPolicy(qt.StrongFocus)
o := qt.NewQObject()
o.OnEventFilter(func(super func(watched *qt.QObject, event *qt.QEvent) bool, watched *qt.QObject, event *qt.QEvent) bool {
if event.Type() == qt.QEvent__KeyPress {
k := qt.UnsafeNewQKeyEvent(event.UnsafePointer())
if k.Key() == int(qt.Key_Return) {
fmt.Println("Ate your Enter key")
return true
}
}
return super(watched, event)
})
l.InstallEventFilter(o)
l.OnKeyPressEvent(func(super func(ev *qt.QKeyEvent), ev *qt.QKeyEvent) {
key := ev.Key()
text := fmt.Sprintf("You pressed key code: " + strconv.Itoa(key))
l.SetText(text)
})
w.Show()
qt.QApplication_Exec()
fmt.Println("OK!")
} This works with either order of operations between |
Beta Was this translation helpful? Give feedback.
I'm working on a precise example that should be of help (see below) but the long and short of it is that my understanding is that you would create a new QObject type in addition to your other object (let's say a QLabel in this instance), no need for a struct:
You would then use
o.OnEventFilter()
to set the filter for the newly-constructed object and then on your original QObject-derived instance, callInstallEventFilter
, so the steps would be effectively:…