Qt-interest Archive, August 2007
How to add custom input device events to Qt?
Message 1 in thread
I want to use a custom input device in my Qt framework. What is the best
place to insert a polling function and where do I generate events for
widget to use this device? Would I have to derive QEventLoop and QEvent?
Arne
--
[ signature omitted ]
Message 2 in thread
Arne Schmitz schrieb:
> I want to use a custom input device in my Qt framework. What is the best
> place to insert a polling function and where do I generate events for
> widget to use this device? Would I have to derive QEventLoop and QEvent?
I once wrote a simple 3D viewer application with "SpaceMouse" support
(some kind of "3D mouse"):
On Unix I overwrote the x11EventFilter for the QApplication. Then simply
instantiate the "SpaceMouseApp_X11" (what a great name) instead of the
ordinary QApplication in the main() function and there you go:
bool SpaceMouseApp_X11::x11EventFilter (XEvent *event) {
SpaceMouseEvent *spaceMouseEvent; // provided by the SpaceMouse SDK
int button;
QWidget *focusWidget;
focusWidget = qApp->focusWidget();
if (focusWidget == 0) {
return false;
}
switch (event->type)
{
case ClientMessage :
switch (MagellanTranslateEvent (display, event, &magellanEvent,
1.0, 1.0))
{
case MagellanInputMotionEvent:
MagellanRemoveMotionEvents (display);
spaceMouseEvent = new SpaceMouseEvent
(CustomEventTypes::SPACEMOUSE_MOVE_EVENT,
magellanEvent.MagellanData[ MagellanX ],
magellanEvent.MagellanData[ MagellanY ],
magellanEvent.MagellanData[ MagellanZ ],
magellanEvent.MagellanData[ MagellanA ],
magellanEvent.MagellanData[ MagellanB ],
magellanEvent.MagellanData[ MagellanC ],
SpaceMouse::NoButton,
magellanButtonState);
qApp->sendEvent (focusWidget, spaceMouseEvent);
case MagellanInputButtonPressEvent:
switch (magellanEvent.MagellanButton) {
case 1:
button = SpaceMouse::ButtonOne;
qDebug ("SpaceMousePressEvent: button 1 pressed.");
break;
case 2:
button = SpaceMouse::ButtonTwo;
qDebug ("SpaceMousePressEvent: button 2 pressed.");
break;
...
...
}
Once you have received an X11 event you process it with whatever SDK you
have available for your input device, translate it into a QCustomEvent
(the "spaceMouseEvent" in my case) and send it to the widget of your
choice (the widget which has "focus", in my case).
This was a Qt 3 based application, but I assume the same idea still
holds for Qt 4. You might want to checkout QApplication::setEventFilter
and/or QApplication::winEventFilter
Cheers, Oliver
Cheers, Oliver
--
[ signature omitted ]
Message 3 in thread
Till Oliver Knoll wrote:
> Arne Schmitz schrieb:
>> I want to use a custom input device in my Qt framework. What is the best
>> place to insert a polling function and where do I generate events for
>> widget to use this device? Would I have to derive QEventLoop and QEvent?
>
> I once wrote a simple 3D viewer application with "SpaceMouse" support
> (some kind of "3D mouse"):
Funny enough, I want to write something similar for the SpaceNavigator, a
follow-up product of your SpaceMouse. :)
Is the SDK the same for those devices, and can you share your code? But
anyway, maybe I just write a new package/class for Qt4...
Arne
--
[ signature omitted ]
Message 4 in thread
Arne Schmitz schrieb:
> Till Oliver Knoll wrote:
> > ...
> Is the SDK the same for those devices, and can you share your code? But
> anyway, maybe I just write a new package/class for Qt4...
All the code I am probably allowed to share I already did in my first
post. In case you are referring to the SDK, see below.
I don't know, it has been 4 years ago or so that I used the SpaceMouse
SDK. I got access to it in my company, don't ask me how the company got
access to it.
In case it is freely available you should be able to find it on the
vendor's web site, it case it is not (and our company paid for it) I
cannot share it for obvious reasons :)
Anyway, I don't know if the SDK would still work with newer devices -
maybe it would at least support the common functionality? Better check
the website...
Cheers, Oliver
--
[ signature omitted ]
Message 5 in thread
Till Oliver Knoll schrieb:
> Arne Schmitz schrieb:
>> Till Oliver Knoll wrote:
>> > ...
> > ...
> I don't know, it has been 4 years ago or so that I used the SpaceMouse
> SDK. I got access to it in my company, don't ask me how the company got
> access to it.
>
> In case it is freely available you should be able to find it on the
> vendor's web site...
And it seems that they indeed offer a download page (didn't read the
license though ;)
http://www.3dconnexion.com/support/4h.php
As for the code in your application: I posted pretty much everything in
my first post already, and when you have a look at the SDK it is pretty
obvious how to get access to the mouse buttons etc.
So basically you do:
- install an event filter (application wide) which reacts to the
SpaceNavigator events (in Qt 3 it was overriding the x11EventFilter or
win32EventFilter (?) methods)
- Query the state of the MouseNavigator with the SDK
- Translate the state into a QCustomEvent of your choice
- Send this custom event to the QWidget of your choice with
QCoreApplication::sendEvent()
Cheers, Oliver
--
[ signature omitted ]
Message 6 in thread
Till Oliver Knoll schrieb:
> Till Oliver Knoll schrieb:
> ..
> - install an event filter (application wide) which reacts to the
> SpaceNavigator events (in Qt 3 it was overriding the x11EventFilter or
> win32EventFilter (?) methods)
This would be what you are looking for on Mac:
http://doc.trolltech.com/4.3/qapplication.html#macEventFilter
and on X11:
http://doc.trolltech.com/4.3/qapplication.html#x11EventFilter
But I have no clue which method you would overwrite on Windows in order
to get access to native Windows events, I didn't find anything which
looks like "winEventFilter" (I only tried the SpaceMouse on Linux back
then) - anyone?
Cheers, Oliver
--
[ signature omitted ]
Message 7 in thread
Till Oliver Knoll wrote:
> But I have no clue which method you would overwrite on Windows in order
> to get access to native Windows events, I didn't find anything which
> looks like "winEventFilter" (I only tried the SpaceMouse on Linux back
> then) - anyone?
Hm, actually there is an OpenSource clone of the Magellan toolkit, which
supplies, similar to the original toolkit, a polling function for querying
the device. If you want to make it as easy as possible, you would poll the
device frequently and then send the events. Because this toolkit already
encapsulates all the X11 or Mac specific behaviour.
Arne
--
[ signature omitted ]