技术开发 频道

Meego touch实现页面跳转和返回

  【IT168 技术】前面写完了第一个meego touch的程序,相信大家也看到Meego touch的界面风格,显示效果等等。不过却没有页面跳转,和返回等效果,那这篇文章就研究下,如何实现页面的跳转和返回?

  Meego touch默认会通过返回键或者退出键实现当前页面的关闭,并且返回到前一个页面。

  所以下面通过代码实现页面的跳转,最后效果如下图:

Meego touch实现页面跳转和返回

Meego touch实现页面跳转和返回

  当程序启动时,会显示第一个页面,点击上面的“Open second page”,则会跳转到第二个页面。

  在第二个页面点击返回按钮,则会关闭当前页面,跳回到第一个页面。

  主要代码介绍:

  main.cpp,程序入口,创建FirstPage的对象,然后使用page->appear(&window);window.show();使第一个页面显示

  #include #include #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 #include 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 #include class FirstPage : public MApplicationPage { Q_OBJECTpublic: FirstPage();private slots: void openNextPage();};#endifSecondPage.cpp,第二个页面的创建,设置窗体标题,添加一个label,并显示I am in second page

  #include “secondpage.h”#include 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 class SecondPage : public MApplicationPage { Q_OBJECTpublic: SecondPage( );};#endif在文件里启动终端,执行qmake -project,生成XXX.pro文件,打开文件,添加CONFIG+=meegotouch,保存退出。

  然后依次执行qmake,make。

  执行完make之后,则会在文件夹下面生成执行文件,通过sudo ./<程序名>,则就会显示出上图所出现的结果。

0
相关文章