Qt6 integration of 3dconnexion spacemouse devices
-
Hello everyone, hope this finds you well.
I have been wondering if there are any updates regarding qt integration of the spacemouse sdk.
I found this old post https://forum.3dconnexion.com/viewtopic.php?f=19&t=4968 but all the source code files are not present anymore and I don't think I'm skilled enough to rewrite it given the information present in the forum (here's the updated link to the post https://www.codegardening.com/post/2011/2011-02-05-using-the-3dconnexion-mouse-with-a-qt-application/) and honestly it looks strange to me that Qt has not yet made any integration packages for 3d connexion devices.
I have tried the basic windows way using window handlers but I can't make it work (no input read) as soon as I pass the handler to the qMainWindow as HWND(mainWindow.winId()), while it works fine if I create a dedicated window to read the data.
Could you please give me some suggestions or point me to some resources?
-
Hi and welcome to devnet,
You should use the way back machine to access the archived version of that page.
-
@LTartarini I used the same example zip file and only had to tweak a little bit to make it run on Qt 5.15.2.
First they split up QObject::setEventFilter into setEventFilter and setNativeEventFilter. We need the latter one. This requires making the Mouse3DInput class a subclass of QAbstractNativeEventFilter
class Mouse3DInput : public QObject, public QAbstractNativeEventFilter { ... }
Now we have to move the implementation of the original bool Mouse3DInput::RawInputEventFilter(void* msg, long* result) function into the definition of the QAbstractNativeEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, qintptr *result) override.
class Mouse3DInput : public QObject, public QAbstractNativeEventFilter { Q_OBJECT public: Mouse3DInput(QWidget* widget); ~Mouse3DInput(); //we have to make it non static and public to match the virtual function bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) override; ... }
//Mouse3DInput.cpp bool Mouse3DInput::nativeEventFilter(const QByteArray &eventType, void *message, long *result) { if (gMouseInput == 0) return false; MSG* msg = (MSG*)(message); if (msg->message == WM_INPUT) { HRAWINPUT hRawInput = reinterpret_cast<HRAWINPUT>(msg->lParam); gMouseInput->OnRawInput(RIM_INPUT,hRawInput); if (result != 0) result = 0; return true; } return false; }
In the constructor we can use our new filter.
Mouse3DInput::Mouse3DInput(QWidget* widget) : QObject(widget) { fLast3dmouseInputTime = 0; InitializeRawInput((HWND)widget->winId()); gMouseInput = this; qApp->installNativeEventFilter(this); }