Как нажать страницу QML при нажатии кнопки в коде С++

Я разрабатываю одно приложение BB 10, в котором я закодировал первую страницу (с NavigationPane) на С++. Теперь я хочу нажать еще одну страницу qml в NavigationPane при нажатии кнопки. Я пробовал следующий код без везения

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
if (!qml->hasErrors()) {
    Page *page = qml->createRootObject<Page>();

    mRoot->push(page);

}

Как я могу этого добиться?


person silwar    schedule 26.02.2013    source источник
comment
Каково содержимое вашего main.qml? Разве вы не должны загружать свою страницу из другого файла?   -  person Dielson Sales    schedule 26.02.2013
comment
В main.qml есть страница с некоторыми кнопками.   -  person silwar    schedule 27.02.2013
comment
это ваша панель mRoot tabpane/navigationpane?   -  person pranavjayadev    schedule 27.02.2013
comment
да mRoot является объектом NavigationPane   -  person silwar    schedule 27.02.2013
comment
трудно сказать, что происходит без журнала ошибок и полного исходного кода, но, пожалуйста, найдите рабочий код в ответе ниже   -  person Sunseeker    schedule 28.02.2013


Ответы (1)


Вот как вы можете нажать страницу, используя NavigationPane в C++:

Исходный файл:

#include <bb/cascades/Application>
#include <bb/cascades/Button>
#include <bb/cascades/Label>
#include <bb/cascades/ActionItem>
#include <bb/cascades/Container>
#include <bb/cascades/DockLayout>
#include <bb/cascades/TitleBar>
#include <bb/cascades/NavigationPaneProperties>
#include "Sandoxproject.hpp"

using namespace bb::cascades;

SandboxApp::SandboxApp(bb::cascades::Application *app)
: QObject(app)
{
    _navPane.reset(NavigationPane::create());
    Page* firstPage = createFirstPage();
    _navPane ->push(firstPage);
    _secondPage.reset(createSecondPage());
    app->setScene(_navPane.data());
}

bb::cascades::Page* SandboxApp::createFirstPage() {
    Page* page = new Page();
    Container* content = new Container();
    TitleBar* titleBar = TitleBar::create().visibility(ChromeVisibility::Visible).title("First Page");
    page->setTitleBar(titleBar);
    content->setLayout(DockLayout::create());
    Button* button = Button::create().text("Go to another page").horizontal(HorizontalAlignment::Center).vertical(VerticalAlignment::Center);
    connect(button, SIGNAL(clicked()), this, SLOT(pushPage()));
    content->add(button);
    page->setContent(content);
    return page;
}

bb::cascades::Page* SandboxApp::createSecondPage() {
    Page* page = new Page();
    TitleBar* titleBar = TitleBar::create().visibility(ChromeVisibility::Visible).title("Second Page");
    page->setTitleBar(titleBar);
    ActionItem* backAction = ActionItem::create();
    connect(backAction, SIGNAL(triggered()), _navPane.data(), SLOT(pop()));
    page->setPaneProperties(NavigationPaneProperties::create().backButton(backAction));
    Container* content = new Container();
    content->setLayout(DockLayout::create());
    content->add(Label::create().text("This is the second page").horizontal(HorizontalAlignment::Center).vertical(VerticalAlignment::Center));
    page->setContent(content);
    return page;
}

void SandboxApp::pushPage() {
    qDebug("pushing another page...");
    _navPane->push(_secondPage.data());
}

Заголовочный файл:

#ifndef Sandoxproject_HPP_
#define Sandoxproject_HPP_

#include <bb/cascades/NavigationPane>
#include <bb/cascades/Page>
#include <QObject>

namespace bb { namespace cascades { class Application; }}

class SandboxApp : public QObject
{
    Q_OBJECT
public:
    SandboxApp(bb::cascades::Application *app);
    virtual ~SandboxApp() {}

private slots:
     void pushPage();

private:
    bb::cascades::Page* createFirstPage();
    bb::cascades::Page* createSecondPage();

    QScopedPointer<bb::cascades::NavigationPane> _navPane;
    QScopedPointer<bb::cascades::Page> _secondPage;

    Q_DISABLE_COPY(SandboxApp);
};


#endif /* Sandoxproject_HPP_ */
person Sunseeker    schedule 28.02.2013