Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
Posts: 23
Registered: ‎10-08-2012
My Device: 9800
Accepted Solution

About integrate QML with C++

[ Edited ]

Hi

I created an class the inherited from Page as following

 

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/Container>
#include <bb/cascades/Button>
#include <bb/cascades/Label>
#include <QObject>
#include <bb/cascades/Page>
#include <bb/cascades/NavigationPane>
#include <bb/cascades/Control>

#include "MyPage.hpp"
#include "MyThirdPage.hpp"
#include "MyEample.hpp"

using namespace bb::cascades;
extern bb::cascades::NavigationPane* mRoot;
MyPage::MyPage() {
	QmlDocument *qml = QmlDocument::create("asset:///mpage.qml");

	Container* control = qml->createRootObject<Container>();
	this->setContent(control);
	qml->setContextProperty("MyPage", this);
}

MyPage::~MyPage() {

}

void MyPage::gotoThirdPage() {
	MyThirdPage* page = new MyThirdPage();
	mRoot->push(page);
}

And here is the header file

/*
 * MyPage.hpp
 *
 *  Created on: ٠٧‏/١٠‏/٢٠١٢
 *      Author: pc
 */

#ifndef MYPAGE_HPP_
#define MYPAGE_HPP_

#include <bb/cascades/Page>
namespace bb { namespace cascades { class Application; }}

class MyPage: public bb::cascades::Page {
public:
	MyPage();
	virtual ~MyPage();
	Q_INVOKABLE void gotoThirdPage();
};

#endif /* MYPAGE_HPP_ */

 

 

 

And I created QML of this page as following

import bb.cascades 1.0

Container {
    layout: StackLayout {
    }
    Label {
        id: tempOutput
        text: "Pressed  times"
    }
    Button {
        text: "Press Me"
        onClicked: {
            MyPage.gotoThirdPage();
        }
    }
}

 Now I want to call gotoThirdPage() that in class MyPage, but it is not invoked, 

qml->setContextProperty("MyPage", this);

Not working

 

Any solution??

 

Thanks

Developer
Posts: 571
Registered: ‎04-11-2009
My Device: BB 10 Dev Alpha

Re: About integrate QML with C++

Hi,

 

In your .hpp or .h file

 

public :

 

Q_INVOKABLE
    void goToThirdPage(); 

Regards,
Kanak Sony
------------------------------------------------------------------------------------------
http://dodevelopnshare.wordpress.com/ | http://www.linkedin.com/profile/view?id=188131481&trk=tab_pro
Contributor
Posts: 23
Registered: ‎10-08-2012
My Device: 9800

Re: About integrate QML with C++

[ Edited ]

I add it, but my problem this line
qml->setContextProperty("MyPage", this);

not working, because I get from qml the main Container, and set page content with is container

Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: About integrate QML with C++

You need to call setContextProperty before you call createRootNode... the qml document context is used to create the root node, so it would not know about the context property in the root if you create the root before you set the context.

Like all of my posts
Contributor
Posts: 23
Registered: ‎10-08-2012
My Device: 9800

Re: About integrate QML with C++

Thanks for your replay

I set 

qml->setContextProperty("MyPage",this);

before

this->setContent(control);

 But also not working.. and "gotoThirdPage" method not invoked

 

 

 

 

Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: About integrate QML with C++

No. like i said you need to call setContextProperty before you call createRootNode... not setContent
Like all of my posts
Contributor
Posts: 23
Registered: ‎10-08-2012
My Device: 9800

Re: About integrate QML with C++

Sorry.. I set 

qml->setContextProperty("MyPage", this);

 Before

Container* control = qml->createRootObject<Container>();

 And Again not working, and "gotoThirdPage" method not invoked

 

 

Thanks

 

 

 

Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: About integrate QML with C++

You also need the Q_OBJECT macro on your class in order for moc to know about your invokable method
Like all of my posts
Contributor
Posts: 23
Registered: ‎10-08-2012
My Device: 9800

Re: About integrate QML with C++

@kylefowler Thanks a lot, it is working greet