Editing Documentation/Maemo 5 Developer Guide/Development Environment/Maemo Programming Environments/Using Maemo 5 specific APIs in Qt application
Warning: You are not logged in.
Your IP address will be recorded in this page's edit history.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 5: | Line 5: | ||
=== Introduction === | === Introduction === | ||
- | This section explains how to use the [[ | + | This section explains how to use the [[Accelerometers | accelerometer]] D-Bus interface in a Maemo 5 Qt application. |
The following code examples are used in this section: | The following code examples are used in this section: | ||
- | * [https:// | + | * [https://garage.maemo.org/svn/maemoexamples/trunk/qt-maemo-gravity-example/ qt-maemo-gravity-example] |
=== D-Bus interface === | === D-Bus interface === | ||
- | It is up to each application how to organize interactions with the [http://maemo.org/api_refs/5.0/beta/mce-dev/ MCE] orientation interface. In [[Documentation/Maemo 5 Developer Guide/Porting Software/Porting Existing GTK+ Application to Maemo 5#Portrait Mode|some cases]] catching orientation signal can be enough, but if more frequent updates are needed | + | It is up to each application how to organize interactions with the [http://maemo.org/api_refs/5.0/beta/mce-dev/ MCE] orientation interface. In [[Documentation/Maemo 5 Developer Guide/Porting Software/Porting Existing GTK+ Application to Maemo 5#Portrait Mode | some cases]] catching orientation signal can be enough, but if, e.g., more frequent updates are needed, you might want to use method calls, as demonstrated in our example. |
- | We've moved D-Bus related functionality and orientation based calculations to a separate library that implements | + | We've moved D-Bus related functionality and orientation based calculations to a separate library that implements `DeviceOrientationNotifier' class. |
Our notifier asks MCE about orientation parameters with a certain period, | Our notifier asks MCE about orientation parameters with a certain period, | ||
- | + | void DeviceOrientationNotifier::timerEvent(QTimerEvent *) | |
- | void DeviceOrientationNotifier::timerEvent(QTimerEvent *) | + | { |
- | { | + | QDBusMessage msg = QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_DEVICE_ORIENTATION_GET); |
- | + | bool sent = QDBusConnection::systemBus().callWithCallback(msg, this, SLOT(mceOrientationSlot(QString, QString, QString, int, int, int))); | |
- | + | } | |
- | } | + | processes answers in `mceOrientationSlot', and emits data needed by the application with the following signal. |
- | + | void deviceOrientation(qreal pitch, qreal roll); | |
- | processes answers in `mceOrientationSlot', and emits data needed by the application with the following signal | + | |
- | + | ||
- | void deviceOrientation(qreal pitch, qreal roll); | + | |
- | + | ||
=== Example application === | === Example application === | ||
Line 31: | Line 27: | ||
We've modified [http://qt.nokia.com/doc/4.5/graphicsview-collidingmice.html one of Qt4 Examples] in order to show how device orientation can be used. | We've modified [http://qt.nokia.com/doc/4.5/graphicsview-collidingmice.html one of Qt4 Examples] in order to show how device orientation can be used. | ||
In our case pitch and roll are scene's characteristics, so we've implemented a `SceneTuner', whose slot can be connected to an appropriate signal in order to setup these scene properties. | In our case pitch and roll are scene's characteristics, so we've implemented a `SceneTuner', whose slot can be connected to an appropriate signal in order to setup these scene properties. | ||
- | + | DeviceOrientationNotifier notifier(100); | |
- | DeviceOrientationNotifier notifier(100); | + | SceneTuner tuner(&scene, TimerInterval); |
- | SceneTuner tuner(&scene, TimerInterval); | + | QObject::connect(¬ifier, SIGNAL(deviceOrientation(qreal, qreal)), &tuner, SLOT(setProps(qreal, qreal))); |
- | QObject::connect(¬ifier, SIGNAL(deviceOrientation(qreal, qreal)), &tuner, SLOT(setProps(qreal, qreal))); | + | |
- | + | ||
Based on that each mouse can take into account the gravity effect, see `Mouse::advance' for details. | Based on that each mouse can take into account the gravity effect, see `Mouse::advance' for details. | ||
== Using Zoom In and Zoom Out keys == | == Using Zoom In and Zoom Out keys == | ||
- | This section | + | This section same qt-maemo-gravity-example to explain how to use Zoom In and Out keys. They are used to set application window to fullsceen and normal mode like in [[Documentation/Maemo 5 Developer Guide/Porting Software/Porting Existing GTK+ Application to Maemo 5|Maemo Sliders]]. |
- | First | + | First keys are released from maemo-status-volume by using <code>XChangeProperty()</code>. |
hardkeyhandler.cpp: | hardkeyhandler.cpp: | ||
- | + | void HardkeyHandler::grabIncreaseDecreaseKeys(QWidget* window, bool grab) | |
- | void HardkeyHandler::grabIncreaseDecreaseKeys(QWidget* window, bool grab) | + | { |
- | { | + | // Tell maemo-status-volume to grab/ungrab increase/decrease keys |
- | + | unsigned long val = (grab==true)?1:0; | |
- | + | Atom atom; | |
- | + | atom = XInternAtom( QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", 0); | |
- | + | XChangeProperty (QX11Info::display(), | |
- | + | window->winId(), | |
- | + | atom, | |
- | + | XA_INTEGER, | |
- | + | 32, | |
- | + | PropModeReplace, | |
- | + | (unsigned char *) &val, | |
- | + | 1); | |
- | + | ||
- | + | ||
- | + | ||
- | |||
- | + | Next handler for keypresses is created. 73 and 74 are keycodes for F7/F8 in SDK and Zoom In/Out buttons in device. Handler functions emit corresponding signals when called. | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | hardkeyhandler.cpp | |
- | } | + | bool HardkeyHandler::processX11Event(XEvent* event) |
- | + | { | |
+ | if ( event->type == KeyPress ) | ||
+ | { | ||
+ | if (event->xkey.keycode == 73 || event->xkey.keycode == QKeySequence::ZoomOut) | ||
+ | { | ||
+ | emit decreaseKeyPressed(); | ||
+ | return true; | ||
+ | } | ||
+ | else if (event->xkey.keycode == 74 || event->xkey.keycode == QKeySequence::ZoomIn) | ||
+ | { | ||
+ | emit increaseKeyPressed(); | ||
+ | return true; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | return false; | ||
+ | } | ||
- | |||
- | myapplication.cpp | + | Signals needs slots, of course. They are located to myapplication.h and myapplication.cpp files. Here changing to fullscreen and back are done by <code>showFullScreen()</code> and <code>showNormal()</code>. |
- | < | + | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | } | + | myapplication.cpp |
+ | void MyApplication::increaseKeyPressed() | ||
+ | { | ||
+ | qDebug("MyApplication::increaseKeyPressed"); | ||
+ | view.showFullScreen(); | ||
+ | |||
+ | } | ||
+ | |||
+ | void MyApplication::decreaseKeyPressed() | ||
+ | { | ||
+ | qDebug("MyApplication::decreaseKeyPressed"); | ||
+ | view.showNormal(); | ||
+ | } | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | + | New signal are connected to corresponding slots in <code>main()</code>. | |
- | main.cpp | + | main.cpp |
- | + | QObject::connect(app.keyHandler(), SIGNAL(decreaseKeyPressed()), &app, SLOT(decreaseKeyPressed())); | |
- | QObject::connect(app.keyHandler(), SIGNAL(decreaseKeyPressed()), &app, SLOT(decreaseKeyPressed())); | + | QObject::connect(app.keyHandler(), SIGNAL(increaseKeyPressed()), &app, SLOT(increaseKeyPressed())); |
- | QObject::connect(app.keyHandler(), SIGNAL(increaseKeyPressed()), &app, SLOT(increaseKeyPressed())); | + | |
- | + | ||
- | Finally | + | Finally gtk-x11 library and new source and header files (hardkeyhandler.* and myapplication.*) are added to project file: |
collidingmice-gravity.pro: | collidingmice-gravity.pro: | ||
- | + | LIBS += -ldevornot-qt-maemo -L/usr/local/lib -lgtk-x11-2.0 | |
- | LIBS += -ldevornot-qt-maemo -L/usr/local/lib -lgtk-x11-2.0 | + | |
- | + | HEADERS += \ | |
- | HEADERS += \ | + | mouse.h \ |
- | + | hardkeyhandler.h \ | |
- | + | myapplication.h \ | |
- | + | scenetuner.h | |
- | + | SOURCES += \ | |
- | SOURCES += \ | + | main.cpp \ |
- | + | hardkeyhandler.cpp \ | |
- | + | myapplication.cpp \ | |
- | + | mouse.cpp | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + |
Learn more about Contributing to the wiki.