I have been playing a bit around with Maemo and writing Qt apps for n900. I ended up needing a rotation aware QMainWindow a couple of times, so I ended up abstracting it away in my own MaemoMainWindow, which I just wanted to share. It has enough ifdefs to also build against ‘normal’ Qt, outside Maemo.
Have fun. Available under any license.
maemomainwindow.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#ifndef MAEMOMAINWINDOW_H #define MAEMOMAINWINDOW_H #include <QMainWindow> class MaemoMainWindow : public QMainWindow { Q_OBJECT public: MaemoMainWindow (QWidget* parent = 0, Qt::WindowFlags flags = 0); virtual ~MaemoMainWindow(); protected: virtual bool event (QEvent* event); Q_SIGNALS: void orientationChanged(Qt::Orientation newOrientation); private Q_SLOTS: void orientationChangedSlot(const QString& newOrientation); private: Qt::Orientation m_orientation; }; #endif // MAEMOMAINWINDOW_H |
maemomainwindow.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#include "maemomainwindow.h" #ifdef Q_WS_MAEMO_5 #include <mce/mode-names.h> #include <mce/dbus-names.h> #endif #ifdef Q_WS_MAEMO_5 #include <QDBusConnection> #include <QDBusMessage> #include <QEvent> #endif MaemoMainWindow::MaemoMainWindow (QWidget* parent, Qt::WindowFlags flags) : QMainWindow (parent, flags) { #ifdef Q_WS_MAEMO_5 QDBusConnection::systemBus().connect(QString(), MCE_SIGNAL_PATH, MCE_SIGNAL_IF, MCE_DEVICE_ORIENTATION_SIG, this, SLOT(orientationChangedSlot(QString))); #endif } MaemoMainWindow::~MaemoMainWindow() { } void MaemoMainWindow::orientationChangedSlot (const QString& newOrientation) { #ifdef Q_WS_MAEMO_5 if (newOrientation == QLatin1String(MCE_ORIENTATION_PORTRAIT)) { setAttribute(Qt::WA_Maemo5ForcePortraitOrientation, true); emit orientationChanged(Qt::Vertical); } else { setAttribute(Qt::WA_Maemo5ForceLandscapeOrientation, true); emit orientationChanged(Qt::Horizontal); } #else Q_UNUSED(newOrientation); #endif } bool MaemoMainWindow::event (QEvent* event) { #ifdef Q_WS_MAEMO_5 switch (event->type()) { case QEvent::WindowActivate: QDBusConnection::systemBus().call( QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_ACCELEROMETER_ENABLE_REQ)); break; case QEvent::WindowDeactivate: QDBusConnection::systemBus().call( QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, MCE_ACCELEROMETER_DISABLE_REQ)); break; default: break; } #endif return QMainWindow::event (event); } |
Update: reformatted code. Thought wordpres and <code> was smart. Thanks Ken.
This post could use some formatting work. In particular, it’s not preformatted, and the HTML seems to have eaten the names of the #include files (and who knows what else)
Just curious: why did you use Q_SIGNALS/Q_SLOTS instead of signals/slots?
So that my headers can be used even with QT_NO_KEYWORDS defined.