Home · All Classes · Main Classes · Deprecated

Application Menu Navigational Pattern Code Example

Those are the three main points for implementing this navigational pattern:

Optionally, as was done in this example, you could hide or disable the menu option (i.e. MApplicationWindow action whose location is set to ApplicationMenu) that corresponds to the page that is currently being displayed.

In this example the actions were grouped in a QActionGroup only because I wanted the same method to be called whenever any of those actions are triggered.

All files can be found at:

libmeegotouch/examples/pagenavigation_menu

main.cpp:

#include <MApplication>

#include "samplewindow.h"

int main(int argc, char **argv)
{
    MApplication app(argc, argv);
    SampleWindow window;

    window.show();

    return app.exec();
}

samplewindow.cpp:

#include "samplewindow.h"

#include <MLabel>

SampleWindow::SampleWindow(QWidget *parent) : MApplicationWindow(parent)
{
    currentAction = 0;

    actionGroup = new QActionGroup(this);

    QAction *alphaAction = createAction("Alpha");
    createAction("Beta");
    createAction("Gamma");
    createAction("Delta");
    createAction("Epsilon");

    connect(actionGroup, SIGNAL(triggered(QAction*)), SLOT(showPageForAction(QAction*)));

    showPageForAction(alphaAction);
}

void SampleWindow::showPageForAction(QAction *action)
{
    if (currentAction == action)
        return;

    MApplicationPage *page = createPage(action->text());
    page->appear(this, MSceneWindow::DestroyWhenDone);

    if (currentAction) {
        currentAction->setVisible(true);
    }

    currentAction = action;
    action->setVisible(false);
}

MApplicationPage *SampleWindow::createPage(const QString &name)
{
    MApplicationPage *page = new MApplicationPage;

    page->setTitle(name);

    QString contentText = QString("%1 Content").arg(name);
    page->setCentralWidget(new MLabel(contentText));

    page->setEscapeMode(MApplicationPageModel::EscapeCloseWindow);

    return page;
}

QAction *SampleWindow::createAction(const QString &name)
{
    MAction *action = new MAction(name, this);
    action->setLocation(MAction::ApplicationMenuLocation);
    action->setActionGroup(actionGroup);
    addAction(action);

    return action;
}

samplewindow.h:

#ifndef SAMPLEWINDOW_H
#define SAMPLEWINDOW_H

#include <MApplicationWindow>

#include <MApplicationPage>
#include <MAction>
#include <QActionGroup>

class SampleWindow : public MApplicationWindow {
    Q_OBJECT

public:
    SampleWindow(QWidget *parent = 0);

public slots:
    void showPageForAction(QAction *action);

private:
    MApplicationPage *createPage(const QString &name);
    QAction *createAction(const QString &name);

    QAction *currentAction;
    QActionGroup *actionGroup;
};

#endif

Copyright © 2010 Nokia Corporation Generated on Thu Nov 4 2010 18:14:23 (PDT)
Doxygen 1.7.1
MeeGo Touch