Meego touch實現頁面跳轉和返回

來源: 米趣網  發布時間: 2010-09-14 15:19  閱讀: 1496 次  推薦: 0   原文鏈接   [收藏]  

前面寫完了第一個Meego touch的程序,相信大家也看到Meego touch的界面風格,顯示效果等等。不過卻沒有頁面跳轉,和返回等效果,那這篇文章就研究下,如何實現頁面的跳轉和返回?
Meego touch默認會通過返回鍵或者退出鍵實現當前頁面的關閉,并且返回到前一個頁面。
所以下面通過代碼實現頁面的跳轉,最后效果如下圖:

當程序啟動時,會顯示第一個頁面,點擊上面的“Open second page”,則會跳轉到第二個頁面。
在第二個頁面點擊返回按鈕,則會關閉當前頁面,跳回到第一個頁面。

主要代碼介紹:
main.cpp,程序入口,創建FirstPage的對象,然后使用page->appear(&window);window.show();使第一個頁面顯示

 
#include <MApplication>
#include <MApplicationWindow>
#include "firstpage.h"

int main(int argc, char **argv)
{
MApplication app(argc, argv);
MApplicationWindow window;
FirstPage
*page = new FirstPage();

page
->appear(&window);
window.show();


return app.exec();
}

FirstPage.cpp,第一個頁面顯示界面的創建,設置窗體標題,增加一個按鈕,顯示Open second page,然后通過connect(button, SIGNAL(clicked()), SLOT(openNextPage()))函數,使button點擊之后,執行openNextPage函數。
在openNextPage()函數里面,創建SecondPage對象,并調用appear方法顯示。

 
#include "firstpage.h"
#include <MSceneManager>
#include <MButton>

FirstPage::FirstPage() : MApplicationPage(0)
{
QString title, buttonTitle,labelTitle;
title
= "First page";
buttonTitle
= "Open second page";

setTitle(title);
MButton
*button = new MButton(buttonTitle);

connect(button, SIGNAL(clicked()), SLOT(openNextPage()));
setCentralWidget(button);
}


void FirstPage::openNextPage()
{
SecondPage
*secondPage = new SecondPage();
secondPage
->appear(scene(), MSceneWindow::DestroyWhenDismissed);
}

firstpage.h

 
#ifndef FIRSTPAGE_H
#define FIRSTPAGE_H

#include <MApplicationPage>
#include <secondpage.h>

class FirstPage : public MApplicationPage {
Q_OBJECT


public:
FirstPage();


private slots:
void openNextPage();
};

#endif

SecondPage.cpp,第二個頁面的創建,設置窗體標題,添加一個label,并顯示I am in second page

 
#include "secondpage.h"
#include <MLabel>

SecondPage::SecondPage() : MApplicationPage(0)
{
QString title, labelTitle;
title
= "Second page";
labelTitle
= "I am in second page";

setTitle(title);
setCentralWidget(
new MLabel(labelTitle));
}

secondpage.h

 
#ifndef SECONDPAGE_H
#define SECONDPAGE_H

#include <MApplicationPage>

class SecondPage : public MApplicationPage {
Q_OBJECT


public:
SecondPage( );
};

#endif

在文件里啟動終端,執行qmake -project,生成XXX.pro文件,打開文件,添加CONFIG+=meegotouch,保存退出。
然后依次執行qmake,make。
執行完make之后,則會在文件夾下面生成執行文件,通過sudo ./<程序名>,則就會顯示出上圖所出現的結果。

0
0
 
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()