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 [[N900 accelerometer|accelerometer]] D-Bus interface in a Maemo 5 Qt application.
+
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://vcs.maemo.org/svn/maemoexamples/tags/maemo_5.0/qt-maemo-gravity-example/ qt-maemo-gravity-example]
+
* [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, for example, you might want to use method calls, as demonstrated in our example.
+
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 <code>DeviceOrientationNotifier</code> class.  
+
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,  
-
<source lang="cpp-qt">
+
void DeviceOrientationNotifier::timerEvent(QTimerEvent *)
-
void DeviceOrientationNotifier::timerEvent(QTimerEvent *)
+
{
-
{
+
  QDBusMessage msg = QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_DEVICE_ORIENTATION_GET);
-
  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)));
-
  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.
-
</source>
+
void deviceOrientation(qreal pitch, qreal roll);
-
processes answers in `mceOrientationSlot', and emits data needed by the application with the following signal:
+
-
<source lang="cpp-qt">
+
-
void deviceOrientation(qreal pitch, qreal roll);
+
-
</source>
+
=== 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.
-
<source lang="cpp-qt">
+
DeviceOrientationNotifier notifier(100);
-
DeviceOrientationNotifier notifier(100);
+
SceneTuner tuner(&scene, TimerInterval);
-
SceneTuner tuner(&scene, TimerInterval);
+
QObject::connect(&notifier, SIGNAL(deviceOrientation(qreal, qreal)), &tuner, SLOT(setProps(qreal, qreal)));
-
QObject::connect(&notifier, SIGNAL(deviceOrientation(qreal, qreal)), &tuner, SLOT(setProps(qreal, qreal)));
+
-
</source>
+
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.
Line 45: Line 39:
hardkeyhandler.cpp:
hardkeyhandler.cpp:
-
<source lang="cpp-qt">
+
void HardkeyHandler::grabIncreaseDecreaseKeys(QWidget* window, bool grab)
-
void HardkeyHandler::grabIncreaseDecreaseKeys(QWidget* window, bool grab)
+
{
-
{
+
    // Tell maemo-status-volume to grab/ungrab increase/decrease keys
-
    // Tell maemo-status-volume to grab/ungrab increase/decrease keys
+
    unsigned long val = (grab==true)?1:0;
-
    unsigned long val = (grab==true)?1:0;
+
    Atom atom;
-
    Atom atom;
+
    atom = XInternAtom( QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", 0);
-
    atom = XInternAtom( QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", 0);
+
    XChangeProperty (QX11Info::display(),
-
    XChangeProperty (QX11Info::display(),
+
                      window->winId(),
-
                    window->winId(),
+
                      atom,
-
                    atom,
+
                      XA_INTEGER,
-
                    XA_INTEGER,
+
                      32,
-
                    32,
+
                      PropModeReplace,
-
                    PropModeReplace,
+
                      (unsigned char *) &val,
-
                    (unsigned char *) &val,
+
                      1);
-
                    1);
+
}
-
}
+
-
</source>
+
Next, a handler for keypresses is created. 73 and 74 are the keycodes for F7/F8 in the SDK and Zoom In/Out buttons in the device. Handler functions emit corresponding signals when called.
Next, a handler for keypresses is created. 73 and 74 are the keycodes for F7/F8 in the SDK and Zoom In/Out buttons in the device. Handler functions emit corresponding signals when called.
-
hardkeyhandler.cpp:
+
hardkeyhandler.cpp
-
<source lang="cpp-qt">
+
bool HardkeyHandler::processX11Event(XEvent* event)
-
bool HardkeyHandler::processX11Event(XEvent* event)
+
{
-
{
+
    if ( event->type == KeyPress )
-
    if ( event->type == KeyPress )
+
    {
-
    {
+
        if (event->xkey.keycode == 73 || event->xkey.keycode == QKeySequence::ZoomOut)
-
        if (event->xkey.keycode == 73 || event->xkey.keycode == QKeySequence::ZoomOut)
+
        {
-
        {
+
            emit decreaseKeyPressed();
-
            emit decreaseKeyPressed();
+
            return true;
-
            return true;
+
        }  
-
        }  
+
        else if (event->xkey.keycode == 74  || event->xkey.keycode ==  QKeySequence::ZoomIn)
-
        else if (event->xkey.keycode == 74  || event->xkey.keycode ==  QKeySequence::ZoomIn)
+
        {
-
        {
+
            emit increaseKeyPressed();
-
            emit increaseKeyPressed();
+
            return true;
-
            return true;
+
        }
-
        }
+
    }
-
    }
+
 +
    return false;
 +
}
-
    return false;
 
-
}
 
-
</source>
 
-
Signals need slots, of course. They are located in <code>myapplication.h</code> and <code>myapplication.cpp</code> files. Here changing to fullscreen and back are done by <code>showFullScreen()</code> and <code>showNormal()</code>.
+
Signals need 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:
+
myapplication.cpp
-
<source lang="cpp-qt">
+
void MyApplication::increaseKeyPressed()
-
void MyApplication::increaseKeyPressed()
+
{
-
{
+
    qDebug("MyApplication::increaseKeyPressed");
-
    qDebug("MyApplication::increaseKeyPressed");
+
    view.showFullScreen();
-
    view.showFullScreen();
+
 +
}
 +
 +
void MyApplication::decreaseKeyPressed()
 +
{
 +
    qDebug("MyApplication::decreaseKeyPressed");
 +
    view.showNormal();
 +
}
-
}
 
-
 
-
void MyApplication::decreaseKeyPressed()
 
-
{
 
-
    qDebug("MyApplication::decreaseKeyPressed");
 
-
    view.showNormal();
 
-
}
 
-
</source>
 
The new signals are connected to corresponding slots in <code>main()</code>.
The new signals are connected to corresponding slots in <code>main()</code>.
-
main.cpp:
+
main.cpp
-
<source lang="cpp-qt">
+
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()));
+
-
</source>
+
-
Finally, <code>gtk-x11</code> library and the new source and header files (hardkeyhandler.* and myapplication.*) are added to the Qt project file:
+
Finally, gtk-x11 library and the new source and header files (hardkeyhandler.* and myapplication.*) are added to the Qt project file:
collidingmice-gravity.pro:
collidingmice-gravity.pro:
-
<pre>
+
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 \
-
    mouse.h \
+
    hardkeyhandler.h \
-
    hardkeyhandler.h \
+
    myapplication.h \
-
    myapplication.h \
+
    scenetuner.h
-
    scenetuner.h
+
SOURCES += \
-
SOURCES += \
+
    main.cpp \
-
    main.cpp \
+
    hardkeyhandler.cpp \
-
    hardkeyhandler.cpp \
+
    myapplication.cpp \
-
    myapplication.cpp \
+
    mouse.cpp
-
    mouse.cpp
+
-
</pre>
+
-
 
+
-
[[Category:Development]]
+
-
[[Category:Documentation]]
+
-
[[Category:Fremantle]]
+

Learn more about Contributing to the wiki.


Please note that all contributions to maemo.org wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see maemo.org wiki:Copyrights for details). Do not submit copyrighted work without permission!


Cancel | Editing help (opens in new window)