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
Developer
Posts: 656
Registered: ‎02-03-2010
My Device: Z10 LE
My Carrier: Rogers
Accepted Solution

How to use cppObject in multiple QML files?

I have a context variable cppObject define as usual :

 

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

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

 

And it works fine in the main.qml.

 

Problem is that it doesn't work in the second QML files  pagetwo.qml  which is created on the fly in main.qml when  button is clicked.


Any idea how to access C++ from pagetwo.qml?

 

Thanks.


Steven Kader at JaredCo
   Follow me on Twitter    |     Mipixi.com :  Web App for Photo Sharing
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: How to use cppObject in multiple QML files?

Hi,

 

Did you remember to import your custom library in the second Page too?

It's easy to forget.

 

If you did can you say what you mean by 'doesn't work', what error/warning  message are you getting from the slogger?

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Highlighted
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: How to use cppObject in multiple QML files?

I've made a simple test app to reproduce this, but I see "HELLO FROM C++" on both pages:

 

main.qml

 

// Navigation pane project template
import bb.cascades 1.0

NavigationPane {
    id: navigationPane
    Page {
        Container {
            Label {
                text: injection.testFunc()
            }
            Button {
                text: "Other page"
                onClicked: {
                    // show detail page when the button is clicked
                    var page = getSecondPage();
                    navigationPane.push(page);
                }
                property Page secondPage
                function getSecondPage() {
                    if (! secondPage) {
                        secondPage = secondPageDefinition.createObject();
                    }
                    return secondPage;
                }
                attachedObjects: [
                    ComponentDefinition {
                        id: secondPageDefinition
                        source: "DetailsPage.qml"
                    }
                ]
            }
        }
    }
}

 

DetailsPage.qml:

 

// Navigation pane project template
import bb.cascades 1.0

Page {
    Container {
        Label {
            text: injection.testFunc()
        }
    }
}

 TestEmpty3.cpp:

 

    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("injection", this);

...

QString TestEmpty3::testFunc()
{
	return "HELLO FROM C++";
}

 TestEmpty3.h:

    Q_INVOKABLE QString testFunc();

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 656
Registered: ‎02-03-2010
My Device: Z10 LE
My Carrier: Rogers

Re: How to use cppObject in multiple QML files?

Thanks for confirming that it should work. I saw that I put my function definition in the wrong place with the Q_INVOKABLE.

Thanks.

Steven Kader at JaredCo
   Follow me on Twitter    |     Mipixi.com :  Web App for Photo Sharing
New Developer
Posts: 6
Registered: ‎03-02-2013
My Device: Z10
My Carrier: BELL

Re: How to use cppObject in multiple QML files?

Hi Zmey, i'm having the same issue and I still have not been able to resolve it.  Would you be able to post all the content from your test project, or provide all all of it (zip) somewhere on the web.  This issue has been driving me nuts, and after reading soooooo many posts I still haven't been able to figure it out why.

 

Thanks

Developer
Posts: 656
Registered: ‎02-03-2010
My Device: Z10 LE
My Carrier: Rogers

Re: How to use cppObject in multiple QML files?


costaf wrote:

Hi Zmey, i'm having the same issue and I still have not been able to resolve it.  Would you be able to post all the content from your test project, or provide all all of it (zip) somewhere on the web.  This issue has been driving me nuts, and after reading soooooo many posts I still haven't been able to figure it out why.

 

Thanks


Is it working from one QML file but not in the second, or not working at all?  Can you show me your .hpp ?


Steven Kader at JaredCo
   Follow me on Twitter    |     Mipixi.com :  Web App for Photo Sharing
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: How to use cppObject in multiple QML files?

Hi,

Here is my test project:

TestEmpty3.zip

 


Andrey Fidrya, @zmeyc on twitter